Cyclic Tour
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total Submission(s): 1143 Accepted Submission(s): 596
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.题意:给出一个有向图,要求把图分为若干个环,每个点只能属于一个环,并且要求全部环上的边的权值和最小,输出最小的权值和。思路:对于环上的每个点,入度=出度,那么我们可以将每个点拆成入点和出点,每个入点和一个出点匹配即可。然后就是用km算法求最小权值和了。注意有重边,应取长度最小的边。AC代码:#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <vector> #include <cmath> #include <cstdlib> #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) #define ll long long #define eps 1e-6 using namespace std; const int maxn=105; const int INF=1000000000; int G[maxn][maxn],lx[maxn],match[maxn],ly[maxn],slack[maxn]; bool visx[maxn],visy[maxn]; int n,m; bool find(int u) { visx[u]=true; for(int i=1;i<=n;i++) { if(visy[i]) continue; if(lx[u]+ly[i]==G[u][i]) { visy[i]=true; if(match[i]==-1||find(match[i])) { match[i]=u; return true; } } else slack[i]=min(slack[i],lx[u]+ly[i]-G[u][i]); } return false; } void KM() { memset(match,-1,sizeof(match)); for(int i=1;i<=n;i++) lx[i]=-INF; memset(ly,0,sizeof(ly)); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) lx[i]=max(lx[i],G[i][j]); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) slack[j]=INF; while(1) { memset(visx,false,sizeof(visx)); memset(visy,false,sizeof(visy)); if(find(i)) break; else { int temp=INF; for(int j=1;j<=n;j++) if(!visy[j]&&temp>slack[j]) temp=slack[j]; for(int j=1;j<=n;j++) { if(visx[j]) lx[j]-=temp; if(visy[j]) ly[j]+=temp; else slack[j]-=temp; } } } } } int main() { int cost,a,b; while(~scanf("%d%d",&n,&m)) { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) G[i][j]=-INF; while(m--) { scanf("%d%d%d",&a,&b,&cost); if(-cost>G[a][b]) G[a][b]=-cost; } KM(); bool flag=true; for(int i=1;i<=n;i++) if(match[i]==-1||G[match[i]][i]==-INF) { printf("-1\n"); flag=false; break; } if(flag) { int ans=0; for(int i=1;i<=n;i++) ans+=G[match[i]][i]; printf("%d\n",-ans); } } return 0; }
费用流解法:#include <iostream> #include <cmath> #include <cstdlib> #include <cstring> #include <cstdio> #include <queue> #include <ctime> #include <algorithm> #define ll __int64 using namespace std; const int INF = 1000000000; const int maxn = 205; struct Edge{ int u, v, cost, cap, flow, next; }et[maxn * maxn]; int pre[maxn], dis[maxn], eh[maxn], low[maxn]; int anscost, ans, s, t, num, n, m; bool vis[maxn]; void init(){ memset(eh, -1, sizeof(eh)); num = 0; } void add(int u, int v, int cost, int cap, int flow){ Edge e = {u, v, cost, cap, flow, eh[u]}; et[num] = e; eh[u] = num++; } void addedge(int u, int v, int cost, int cap){ add(u, v, cost, cap, 0); add(v, u, -cost, 0, 0); } bool spfa(){ queue<int>Q; fill(&dis[0], &dis[maxn], INF); memset(vis, false, sizeof(vis)); memset(pre, -1, sizeof(pre)); low[s] = INF, vis[s] = true, dis[s] = 0; Q.push(s); while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = false; for(int i = eh[u]; i != -1; i = et[i].next) { int v = et[i].v, cost = et[i].cost, cap = et[i].cap, flow = et[i].flow; if(cap - flow && dis[v] > dis[u] + cost) { dis[v] = dis[u] + cost; pre[v] = i; low[v] = min(cap - flow, low[u]); if(!vis[v]) { Q.push(v); vis[v] = true; } } } } return dis[t] != INF; } void costflow(){ ans = anscost = 0; while(spfa()) { int x = pre[t]; ans += low[t]; anscost += low[t] * dis[t]; while(x != -1) { et[x].flow += low[t]; et[x^1].flow -= low[t]; x = pre[et[x].u]; } } } int main() { int a, b, cost; while(~scanf("%d%d", &n, &m)) { init(); s = 0, t = 2 * n + 1; while(m--) { scanf("%d%d%d", &a, &b, &cost); addedge(a, b + n, cost, 1); } for(int i = 1; i <= n; i++) { addedge(s, i, 0, 1); addedge(i + n, t, 0, 1); } costflow(); if(ans < n) printf("-1\n"); else printf("%d\n", anscost); } return 0; }