Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 92 Accepted Submission(s): 50
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 真的剛開始沒想到這道題目能用費用流去做啊。。。。 後來看了大牛的題解才想到 幾個很關鍵的條件 1.每個點只屬於一個迴路,並且所有點必須都屬於迴路 2.每個迴路必須要有兩個以上的點 3.要是迴路總的費用最小 這幾個條件限制了這題的解法 可以是KM,為了讓KM演算法更熟悉一點,明天就會上KM的解題報告 說說最小費用流的構圖方法吧 首先是設立兩個超級源點匯點S,T S向每個點連邊,容量為1,費用為0,限制的是每個點的入度 T和每個I+N連邊,容量為1,費用為0,限制的是每個點的出度 這樣就滿足了第一個條件,就是當最大流為N的時候,就可以構造這麼一個迴路 接著費用就比較簡單了,因為費用在每個邊上,所以當兩點之間存在邊的時候,構造邊 邊的容量是1,費用是題目所給,別忘了給反向邊 #include<cstdio> #include<cstring> #include<queue> #define min(a,b) a>b?b:a using namespace std; const int MAX = 110; const int INF = 0x7fffffff; int N, M, K; int SS, TT; int mm[2 * MAX][2 * MAX]; //flow map int cc[2 * MAX][2 * MAX]; //cost map int prev[2 * MAX];//下面4個是必須要的 int flow[2 * MAX]; int dis[2 * MAX]; int in[2 * MAX]; int map[110][110]; //主要對這兩個函數進行修改 void ready() { memset(map,0,sizeof(map)); for(int i=1;i<=M;i++) { int x,y,c; scanf("%d%d%d",&x,&y,&c); if(map[x][y]==0) map[x][y]=c; else map[x][y]=min(map[x][y],c); } } void makeMap() { SS = 0, TT = N + N + 1; memset(mm, 0, sizeof(mm)); for(int i = 1; i <= N; i++) mm[SS][i] = 1;//限制入度和出度,想想題目的限制 for(int i = 1+N; i <= N+N; i++) mm[i][TT] = 1; for(int i = 1; i <= N; i++) for(int j = 1; j <= N; j++) if(map[i][j]!=0) mm[i][j + N] = 1; //make the cost map memset(cc, 0, sizeof(cc)); for(int i = 1; i <= N; i++) for(int j = 1; j <= N; j++) if(map[i][j]) { cc[i][j + N] = map[i][j]; cc[j + N][i] = -map[i][j]; //構造費用流的負權邊 } } int spfa() { memset(in, 0, sizeof(in)); memset(dis, -1, sizeof(dis)); memset(prev, -1, sizeof(prev)); memset(flow, 0, sizeof(flow)); in[SS] = 1, dis[SS] = 0, flow[SS] = INF; queue<int> q; q.push(SS); while(!q.empty()) { int u = q.front(); q.pop(); in[u] = 0; for(int j = SS; j <= TT; j++) { if(mm[u][j] > 0 && (dis[j] == -1 || dis[u] + cc[u][j] < dis[j])) { dis[j] = dis[u] + cc[u][j]; //in[j] = 1; prev[j] = u; flow[j] = min(flow[u], mm[u][j]); if(in[j] == 0) in[j] = 1, q.push(j); } } } return flow[TT]; } int mcmf() { int res = 0,flow=0; while(1) { int now = spfa(); if(now == 0) break; int u = TT; flow+=now; while(u != SS) { int v = prev[u]; mm[v][u] -= now; mm[u][v] += now; res += now * cc[v][u]; u = v; } } //printf("flow=%d/n",flow); if(flow==N) return res; else return -1; } int main() { while(scanf("%d%d", &N, &M)!=EOF) { ready(); makeMap(); int t=mcmf(); printf("%d/n", t); } return 0; }