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).
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 -1HintIn the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.
Author
RoBa@TJU
Source
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
#define maxn 550
#define INF 99999999
int prev[maxn];
bool vis[maxn];
int head[maxn];
int ep,flow;
int Dist[maxn];
struct ele
{
int u;
int v;
int w;
int c;
int next;
}edge[200*maxn];
void Add(int u,int v,int w,int c)
{
edge[ep].u=u;
edge[ep].v=v;
edge[ep].w=w;
edge[ep].c=c;
edge[ep].next=head[u];
head[u]=ep++;
edge[ep].u=v;
edge[ep].v=u;
edge[ep].w=-w;
edge[ep].c=0;
edge[ep].next=head[v];
head[v]=ep++;
}
int spfa(int S,int E)
{
int i;
int v;
queue<int>Q;
int u;
for(i=0;i<maxn;i++)
{
Dist[i]=INF;
prev[i]=-1;
vis[i]=false;
}
vis[S]=true;
Dist[S]=0;
Q.push(S);
while(!Q.empty())
{
u=Q.front();
Q.pop();
vis[u]=false;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(edge[i].c&&Dist[u]+edge[i].w<Dist[v])
{
Dist[v]=Dist[u]+edge[i].w;
prev[v]=i;
if(!vis[v])
{
Q.push(v);
vis[v]=true;
}
}
}
}
if(Dist[E]==INF)
return 0;
return 1;
}
int EK(int S,int E)
{
int u;
int sum=0;
int temp;
while(spfa(S,E))
{
// printf("88888\n");
sum+=Dist[E];
temp=INF;
u=E;
while(u!=S)
{
if(temp>edge[prev[u]].c)
temp=edge[prev[u]].c;
u=edge[prev[u]].u;
}
flow+=temp;
u=E;
while(u!=S)
{
edge[prev[u]].c-=temp;
edge[prev[u]^1].c+=temp;
u=edge[prev[u]].u;
}
}
return sum;
}
int main()
{
int n,m;
int i;
int x,y,z;
while(scanf("%d%d",&n,&m)!=EOF)
{
ep=0;
memset(head,-1,sizeof(head));
flow=0;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
Add(x,n+y,z,1);
}
for(i=1;i<=n;i++)
{
Add(0,i,0,1);
Add(n+i,2*n+1,0,1);
}
int ans=EK(0,2*n+1);
if(flow==n)
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}
#include<string.h>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
#define maxn 550
#define INF 99999999
int prev[maxn];
bool vis[maxn];
int head[maxn];
int ep,flow;
int Dist[maxn];
struct ele
{
int u;
int v;
int w;
int c;
int next;
}edge[200*maxn];
void Add(int u,int v,int w,int c)
{
edge[ep].u=u;
edge[ep].v=v;
edge[ep].w=w;
edge[ep].c=c;
edge[ep].next=head[u];
head[u]=ep++;
edge[ep].u=v;
edge[ep].v=u;
edge[ep].w=-w;
edge[ep].c=0;
edge[ep].next=head[v];
head[v]=ep++;
}
int spfa(int S,int E)
{
int i;
int v;
queue<int>Q;
int u;
for(i=0;i<maxn;i++)
{
Dist[i]=INF;
prev[i]=-1;
vis[i]=false;
}
vis[S]=true;
Dist[S]=0;
Q.push(S);
while(!Q.empty())
{
u=Q.front();
Q.pop();
vis[u]=false;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(edge[i].c&&Dist[u]+edge[i].w<Dist[v])
{
Dist[v]=Dist[u]+edge[i].w;
prev[v]=i;
if(!vis[v])
{
Q.push(v);
vis[v]=true;
}
}
}
}
if(Dist[E]==INF)
return 0;
return 1;
}
int EK(int S,int E)
{
int u;
int sum=0;
int temp;
while(spfa(S,E))
{
// printf("88888\n");
sum+=Dist[E];
temp=INF;
u=E;
while(u!=S)
{
if(temp>edge[prev[u]].c)
temp=edge[prev[u]].c;
u=edge[prev[u]].u;
}
flow+=temp;
u=E;
while(u!=S)
{
edge[prev[u]].c-=temp;
edge[prev[u]^1].c+=temp;
u=edge[prev[u]].u;
}
}
return sum;
}
int main()
{
int n,m;
int i;
int x,y,z;
while(scanf("%d%d",&n,&m)!=EOF)
{
ep=0;
memset(head,-1,sizeof(head));
flow=0;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
Add(x,n+y,z,1);
}
for(i=1;i<=n;i++)
{
Add(0,i,0,1);
Add(n+i,2*n+1,0,1);
}
int ans=EK(0,2*n+1);
if(flow==n)
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}