# Cyclic Tour

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 1688    Accepted Submission(s): 859

Problem Description

There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total length of all the tours minimum, but he is too lazy to calculate. Can you help him?

Input

There are several test cases in the input. You should process to the end of file (EOF).
The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).

Output

Output one number for each test case, indicating the minimum length of all the tours. If there are no such tours, output -1.

Sample Input

6 9
1 2 5
2 3 5
3 1 10
3 4 12
4 1 8
4 6 11
5 4 7
5 6 9
6 5 4
6 5
1 2 1
2 3 1
3 4 1
4 5 1
5 6 1

Sample Output

42
-1

Hint

In the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.

可以发现，每个点的入度和出度都是1。

```#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <utility>
#include <queue>
#include <stack>
using namespace std;
const int INF=1e9;
const double eps=1e-6;
const int N = 210;

int nx,ny;
int g[N][N];
int linker[N],lx[N],ly[N];// x is outpoint, y is inpoint
int slack[N];
int visx[N],visy[N];

int n,m;

bool DFS(int x)
{
visx[x]=true;
for(int y=0;y<ny;y++)
{
if(visy[y]) continue;
int tmp = lx[x]+ly[y]-g[x][y];
if(tmp==0)
{
visy[y]=true;
{
return true;
}
}
else if(slack[y]>tmp)
slack[y]=tmp;
}
return false;
}

int KM()
{
memset(ly,0,sizeof(ly));
for(int i=0;i<nx;i++)
{
lx[i]=-INF;
for(int j=0;j<ny;j++)
if(g[i][j]>lx[i])
lx[i]=g[i][j];
}
for(int x=0;x<nx;x++)
{
for(int i=0;i<ny;i++)
slack[i]=INF;
while(true)
{
memset(visx,false,sizeof(visx));
memset(visy,false,sizeof(visy));
if(DFS(x)) break;
int d = INF;
for(int i=0;i<ny;i++)
if(!visy[i] && d>slack[i])
d=slack[i];
for(int i=0;i<nx;i++)
if(visx[i])
lx[i]-=d;
for(int i=0;i<ny;i++)
{
if(visy[i]) ly[i]+=d;
else slack[i]-=d;
}
}
}
int res = 0, cnt = 0;
for(int i=0;i<ny;i++)
{
continue;
cnt++;
}
if(cnt!=nx) return -1;
return -res;
}

void run()
{
//    memset(g,0,sizeof(g));
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
g[i][j]=-INF;
int u,v,c;
while(m--)
{
scanf("%d%d",&u,&v);
scanf("%d",&c);
if(-c>g[u-1][v-1])
g[u-1][v-1]=-c;
}
nx=ny=n;
printf("%d\n",KM());
}

int main()
{
#ifdef LOCAL
freopen("case.txt","r",stdin);
#endif // LOCAL
while(scanf("%d%d",&n,&m)!=EOF)
run();
return 0;
}```

## POJ2584 T-Shirt Gumbo 二分图匹配（网络流）

1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 5 const int inf=0x3f3f3f3f; 6 const int sink=30; 7 8 struct Edge 9 { 10 int to; 11 int next; 12 int capacity; 13 14 void assign(int t,int n,int c) 15 { 16 to=t; next=n; ca

## HDU 1853 Cyclic Tour（最小费用最大流）

Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1879    Accepted Submission(s): 938 Problem Description There are N cities in our country, and M one-way roads connecting them. Now L