Cyclic Tour
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 914 Accepted Submission(s): 466
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.
Author
RoBa@TJU
Source
HDU 2007 Programming Contest - Final
Recommend
lcy
第一次做费用流~~WA了几次~~~数组要大~~
思路:
环:则入度==出度
拆点建图,费用是长度。
感觉好新鲜。。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define inf 1000000000
using namespace std;
struct node
{
int u,v,flow,cost;
};
node e[50100];
int first[505],next[50100],preedge[505],p[505],d[505];
int ans_flow=0,ans_cost=0;
//int cc;
void add_edge(int u,int v,int f,int c,int &cc)
{
e[cc].u=u;
e[cc].v=v;
e[cc].flow=f;
e[cc].cost=c;
next[cc]=first[u];
first[u]=cc;
cc++;
e[cc].u=v;
e[cc].v=u;
e[cc].flow=0;
e[cc].cost=-c;
next[cc]=first[v];
first[v]=cc;
cc++;
}
bool spfa(int s,int t)
{
int inq[205];
memset(inq,0,sizeof(inq));
memset(p,-1,sizeof(p));
memset(preedge,-1,sizeof(preedge));
int i;
for(i=0;i<=t;i++)
d[i]=inf;
d[0]=0;
queue<int> q;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
inq[u]=0;
for(int i=first[u];i!=-1;i=next[i])
{
if(e[i].flow)
{
int v=e[i].v;
if(d[v]>d[u]+e[i].cost)
{
p[v]=u;
preedge[v]=i;
d[v]=d[u]+e[i].cost;
if(!inq[v])
{
inq[v]=1;
q.push(v);
}
}
}
}
}
if(d[t]>=inf)
return false;
else
return true;
}
void min_cost_flow(int s,int t)
{
int u;
ans_flow=0,ans_cost=0;
while(spfa(s,t))
{
u=t;
int mm=inf;
while(p[u]!=-1)
{
mm=min(mm,e[preedge[u]].flow);
u=p[u];
}
u=t;
while(p[u]!=-1)
{
e[preedge[u]].flow-=mm;
e[preedge[u]^1].flow+=mm;
u=p[u];
}
ans_cost+=d[t]*mm;
ans_flow+=mm;
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int i;
memset(first,-1,sizeof(first));
memset(next,-1,sizeof(next));
memset(e,0,sizeof(e));
int s=0;
int t=2*n+1;
int cc=0;
for(i=0;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add_edge(u,n+v,1,w,cc);
}
for(i=1;i<=n;i++)
{
add_edge(0,i,1,0,cc);
add_edge(n+i,t,1,0,cc);
}
min_cost_flow(s,t);
//printf("%d\n",ans_flow);
if(ans_flow==n)
printf("%d\n",ans_cost);
else
printf("-1\n");
}
return 0;
}