Cyclic Tour
Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/65535K (Java/Other)
Total Submission(s) : 2 Accepted Submission(s) : 2
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 -1题意:给定一个有向图,问这个图中所有环的的路径和的和最小值。每个点只属于一个环,并且每个环至少两个点。、HintIn the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.思路:费用流。建图时要拆点,然后两个相连的点i与j,则建立边(i,j+n)流量为1,费用即为题中所给路程。源点与i建边,费用0,流量为1.i+n与汇点建边,流量为1,费用为0。这样保证图的入度等于出度,即保证每个点只属于一个环。#include<iostream> #include<stdio.h> #include<string.h> #include<queue> using namespace std; #define inf 0xfffffff #define M 1005 int m,n; struct edge { int u,v,flow,c,next; }edge[9999999]; int vis[M],dis[M],head[M],pre[M]; int tot,flow_sum; void addedge(int u,int v,int c,int f) { edge[tot].u=u; edge[tot].v=v; edge[tot].c=c; edge[tot].flow=f; edge[tot].next=head[u]; head[u]=tot++; edge[tot].u=v; edge[tot].v=u; edge[tot].c=-c; edge[tot].flow=0; edge[tot].next=head[v]; head[v]=tot++; } bool spfa(int st,int end) { int u,v; queue<int>q; for(int i=0;i<=end+2;i++) { pre[i]=-1; vis[i]=0; dis[i]=inf; } vis[st]=1; dis[st]=0; q.push(st); while(!q.empty()) { u=q.front(); q.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=edge[i].next) { if(edge[i].flow>0) { v=edge[i].v; if(dis[v]>dis[u]+edge[i].c) { dis[v]=dis[u]+edge[i].c; pre[v]=i; if(!vis[v]) { vis[v]=1; q.push(v); } } } } } return dis[end]!=inf; } int MCMF(int st,int end) { int ans=0,flow,i; flow_sum=0; while(spfa(st,end)) { flow=inf; for(i=pre[end];i!=-1;i=pre[edge[i].u]) if(edge[i].flow<flow) flow=edge[i].flow; for(i=pre[end];i!=-1;i=pre[edge[i].u]) { edge[i].flow-=flow; edge[i^1].flow+=flow; } ans+=dis[end]; flow_sum+=flow; } if(flow_sum==n) return ans; else return -1; } int main() { int a,b,c; while(scanf("%d%d",&n,&m)!=EOF) { tot=0; memset(head,-1,sizeof(head)); for(int i=0;i<m;i++) { scanf("%d %d %d",&a,&b,&c); addedge(a,b+n,c,1); } for(int i=1;i<=n;i++) { addedge(0,i,0,1); addedge(i+n,2*n+1,0,1); } cout<<MCMF(0,2*n+1)<<endl; } return 0; } /* 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 */