HDU 1853--Cyclic Tour【最小費用最大流 && 有向環最小權值覆蓋 】,hdu1853--cyclic
Cyclic Tour
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 1950 Accepted Submission(s): 984
Problem DescriptionThere 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?
InputThere 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).
OutputOutput 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 91 2 52 3 53 1 103 4 124 1 84 6 115 4 75 6 96 5 46 51 2 12 3 13 4 14 5 15 6 1
Sample Output
42-1Hint In the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.
和HDU 3488同類型的題,具體的解析請看 :HDU3488
水一遍熟悉熟悉模板。
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#define INF 0x3f3f3f3f#define maxn 500#define maxm 77000using namespace std;int n, m;int outset;int inset;struct node { int u, v, cap, flow, cost, next;};node edge[maxm];int head[maxn], cnt;int per[maxn];//記錄增廣路徑上 到達點i的邊的編號int dist[maxn], vis[maxn];void init(){ cnt = 0; memset(head, -1, sizeof(head));}void add(int u, int v, int w, int c){ int i; for(i = head[u]; i != -1; i = edge[i].next){ node E = edge[i]; if(v == E.v) break; } if(i != -1){ if(edge[i].cost > c) edge[i].cost = c, edge[i ^ 1].cost = -c; return ; } //edge[cnt] = {u, v, w, 0, c, head[u]}這樣寫就直接逾時了。 node E1 = {u, v, w, 0, c, head[u]}; edge[cnt] = E1; head[u] = cnt++; node E2 = {v, u, 0, 0, -c, head[v]}; edge[cnt] = E2; head[v] = cnt++;}void getmap(){ outset = 0; inset = n * 2 + 1; for(int i = 1; i <= n; ++i){ add(outset, i, 1, 0); add(i + n, inset, 1, 0); } while(m--){ int a, b, c; scanf("%d%d%d", &a, &b, &c); add(a, b + n, 1, c); }}bool SPFA(int st, int ed){ queue<int>q; for(int i = 0; i <= inset; ++i){ dist[i] = INF; vis[i] = 0; per[i] = -1; } dist[st] = 0; vis[st] = 1; q.push(st); while(!q.empty()){ int u = q.front(); q.pop(); vis[u] = 0; for(int i = head[u]; i != -1; i = edge[i].next){ node E = edge[i]; if(dist[E.v] > dist[u] + E.cost && E.cap > E.flow){//可以鬆弛 且 沒有滿流 dist[E.v] = dist[u] + E.cost; per[E.v] = i;//記錄到達這個點的邊的編號 if(!vis[E.v]){ vis[E.v] = 1; q.push(E.v); } } } } return per[ed] != -1;}void MCMF(int st, int ed, int &cost, int &flow){ flow = 0;//總流量 cost = 0;//總費用 while(SPFA(st, ed)){//每次尋找花銷最小的路徑 int mins = INF; for(int i = per[ed]; i != -1; i = per[edge[i ^ 1].v]){ mins = min(mins, edge[i].cap - edge[i].flow); } //增廣 for(int i = per[ed]; i != -1; i = per[edge[i ^ 1].v]){ edge[i].flow += mins; edge[i ^ 1].flow -= mins; cost += edge[i].cost * mins; } flow += mins; }}int main (){ while(scanf("%d%d", &n, &m) != EOF){ init(); getmap(); int cost, flow; MCMF(outset, inset, cost, flow); if(flow == n) printf("%d\n", cost); else printf("-1\n"); } return 0;}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。