The Chinese Postman Problem HIT - 2739(有向圖中國郵路問題)
阿新 • • 發佈:2018-10-07
nbsp != const 完美 cout while add open ems
無向圖的問題,如果每個點的度數為偶數,則就是歐拉回路,而對於一個點只有兩種情況,奇數和偶數,那麽就把都為奇數的一對點 連一條 邊權為原圖中這兩點最短路的值 的邊 是不是就好了
無向圖中國郵路問題:
有向圖的問題,如果每個點的入度和出度相同,則就是歐拉回路,而這個情況就多了,相同、入度少一、入度少倆·····、出度少1、出度少倆,
吶 如果我們把入度少的 和 出度少的連起來是不是就是歐拉回路了,比如說點x的出度為7,入度為3;點y的出度為2,入度為4;點z的出度為2,入度為4;
那麽x是連點y還是點z,當然是先連距離最小的那個,假設是y,那麽x <- y 連兩條邊之後,x入度為7,入度為5,y的入度和出度相同,
那麽x就開始連z,仔細想一想 這是不是就是費用流,先使路的費用小的滿流,然後次小,然後次次小,所以費用流可以完美解決這個問題
有向圖的中國郵路問題:
咳咳。。。反正wrong 交網上的代碼也wrong
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include<stack> #include <queue> #include <algorithm> #include <cmath> #include <bitset> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #definerd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define pd(a) printf("%d\n", a); #define plld(a) printf("%lld\n", a); #define pc(a) printf("%c\n", a); #define ps(a) printf("%s\n", a); #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 30010, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff; int n, m, s, t; int head[maxn], d[maxn], vis[maxn], p[maxn], f[maxn], fi[maxn]; int in[maxn], out[maxn]; int cnt, flow, value; struct node { int u, v, c, w, next; }Node[maxn << 1]; void add(int u, int v, int c, int w) { Node[cnt].u = u; Node[cnt].v = v; Node[cnt].w = w; Node[cnt].c = c; Node[cnt].next = head[u]; head[u] = cnt++; } int spfa() { queue<int> Q; mem(vis, 0); mem(p, -1); for(int i = 0; i < maxn; i++) d[i] = INF; Q.push(s); d[s] = 0; vis[s] = 1; p[s] = 0, f[s] = INF; while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = 0; for(int i = head[u]; i != -1; i = Node[i].next) { node e = Node[i]; if(d[e.v] > d[u] + e.w && e.c > 0) { d[e.v] = d[u] + e.w; p[e.v] = i; f[e.v] = min(f[u], e.c); if(!vis[e.v]) { Q.push(e.v); vis[e.v] = 1; } } } } if(p[t] == -1) return 0; flow += f[t]; value += f[t] * d[t]; for(int i = t; i != s; i = Node[p[i]].u) { Node[p[i]].c -= f[t]; Node[p[i]^1].c += f[t]; } return 1; } void max_flow() { while(spfa()); } void init() { mem(head, -1); mem(in, 0); mem(out, 0); cnt = value = flow = 0; } int find(int x) { return fi[x] == x ? fi[x] : (fi[x] = find(fi[x])); } int main() { int T; int u, v, w; cin >> T; while(T--) { for(int i = 0; i < maxn; i++) fi[i] = i; int flag = 0, ans = 0; init(); int edge_sum = 0; cin >> n >> m; s = n + 1, t = n + 2; for(int i = 0; i < m; i++) { cin >> u >> v >> w; int l = find(u); int r = find(v); if(l != r) fi[l] = r; edge_sum += w; add(u, v, INF, w); in[v]++; out[u]++; } for(int i = 0; i < n; i++) if(fi[i] == i) ans++; if(ans > 1) { puts("-1"); continue; } int tot_flow = 0; for(int i = 0; i < n; i++) { if(in[i] == 0 && out[i] == 0) { flag = 1; break; } if(out[i] > in[i]) add(i, t, out[i] - in[i], 0), tot_flow += out[i] - in[i]; else if(in[i] > out[i]) add(s, i, in[i] - out[i], 0); } if(flag) { puts("-1"); continue; } max_flow(); if(tot_flow != flow) { puts("-1"); continue; } cout << edge_sum + value << endl; } return 0; }
The Chinese Postman Problem HIT - 2739(有向圖中國郵路問題)