HDU 3488--Tour【最小費用最大流 && 有向環最小權值覆蓋 && 經典】,hdu3488--tour
Tour
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2308 Accepted Submission(s): 1156
Problem DescriptionIn the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one-way roads connecting them. You are lucky enough to have a chance to have a tour in the kingdom. The route should be designed as: The route should contain one or more loops. (A loop is a route like: A->B->……->P->A.)
Every city should be just in one route.
A loop should have at least two cities. In one route, each city should be visited just once. (The only exception is that the first and the last city should be the same and this city is visited twice.)
The total distance the N roads you have chosen should be minimized.
InputAn integer T in the first line indicates the number of the test cases.
In each test case, the first line contains two integers N and M, indicating the number of the cities and the one-way roads. Then M lines followed, each line has three integers U, V and W (0 < W <= 10000), indicating that there is a road from U to V, with the distance of W.
It is guaranteed that at least one valid arrangement of the tour is existed.
A blank line is followed after each test case.
OutputFor each test case, output a line with exactly one integer, which is the minimum total distance.
Sample Input
16 91 2 52 3 53 1 103 4 124 1 84 6 115 4 75 6 96 5 4
Sample Output
42
題意:
給出n個點m條單向邊邊以及經過每條邊的費用,讓你求出走過一個哈密頓環(除起點外,每個點只能走一次)的最小費用。題目保證至少存在一個環滿足條件。
解析:
任意類似的【有向環最小權值覆蓋】問題,都可以用最小費用流來寫。
由於題目中要求每個點最多走一次,為了防止走多次的發生,我們要把每個點 i 拆成左點i 和 右點i + n兩個點。
具體建圖如下:
源點outset編號0, 所有左點編號 1~n ,右點編號 n+1 ~ 2*n, 匯點inset 編號 2*n+1.
(1)源點outset到第i個點有邊 ( outset, i, 1, 0),即源點和左點建邊。
(2)如果從 i 點到 j 點有權值為 c 的邊,那麼有邊 (i, j+n, 1, c),即左點和右點建邊, 確保了每個點只走一次。
(3)每個節點到匯點有邊 (i+n, inset, 1, 0), 即右點和匯點建邊。
最終如果最大流 == n 的話(即滿流),那麼最小費用就是我們所求,這裡題目確保有解,所以下面的代碼我就沒判斷。
為什麼這樣的構圖方法就可以求得我們所要的解, 具體解析請點這裡:解析
而且本題時間要求比較嚴格,各種逾時,需要加個去重邊處理才行。
#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(){ scanf("%d%d", &n, &m); 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;// memset(dist, INF, sizeof(dist));// memset(vis, 0, sizeof(vis));// memset(per, -1, sizeof(per)); 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 (){ int T; scanf("%d", &T); while(T--){ init(); getmap(); int cost, flow; MCMF(outset, inset, cost, flow); printf("%d\n", cost); } return 0;}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。