【题目链接】
在最短路图上跑最小割。
用dijkstra写WA了十几发,不知道什么问题,换成Floyd就过了。
/* Pigonometry */ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 505, maxm = maxn * maxn, inf = 0x3f3f3f3f, maxq = 10000; int n, m, head[maxn], cur[maxn], cnt, bg, ed, mp[maxn][maxn], depth[maxn], q[maxq]; struct _edge { int v, w, next; } g[maxm << 1]; struct _data { int u, v, w, c; } e[maxm]; inline int iread() { int f = 1, x = 0; char ch = getchar(); for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1; for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; return f * x; } inline void add(int u, int v, int w) { g[cnt] = (_edge){v, w, head[u]}; head[u] = cnt++; } inline bool bfs() { for(int i = 0; i <= n; i++) depth[i] = -1; int h = 0, t = 0, u, i; depth[q[t++] = bg] = 1; while(h != t) for(i = head[u = q[h++]]; ~i; i = g[i].next) if(g[i].w && !~depth[g[i].v]) { depth[g[i].v] = depth[u] + 1; if(g[i].v == ed) return 1; q[t++] = g[i].v; } return 0; } inline int dfs(int x, int flow) { if(x == ed) return flow; int left = flow; for(int i = cur[x]; ~i; i = g[i].next) if(g[i].w && depth[g[i].v] == depth[x] + 1) { int tmp = dfs(g[i].v, min(left, g[i].w)); left -= tmp; g[i].w -= tmp; g[i ^ 1].w += tmp; if(g[i].w) cur[x] = i; if(!left) return flow; } if(left == flow) depth[x] = -1; return flow - left; } int main() { n = iread(); m = iread(); for(int i = 1; i <= n; i++) head[i] = -1; cnt = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(i ^ j) mp[i][j] = inf; for(int i = 1; i <= m; i++) { int u = iread(), v = iread(), w = iread(), c = iread(); mp[u][v] = mp[v][u] = w; e[i] = (_data){u, v, w, c}; } for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]); printf("%d\n", mp[1][n]); for(int i = 1; i <= m; i++) { int u = e[i].u, v = e[i].v; if(mp[1][u] + e[i].w + mp[v][n] == mp[1][n]) add(u, v, e[i].c), add(v, u, 0); if(mp[1][v] + e[i].w + mp[u][n] == mp[1][n]) add(v, u, e[i].c), add(u, v, 0); } bg = 1; ed = n; int ans = 0; while(bfs()) { for(int i = 0; i <= n; i++) cur[i] = head[i]; ans += dfs(bg, 0x7fffffff); } printf("%d\n", ans); return 0; }
2016.4.22,看了数据才发现,第三个数据点有重边= =,所以只能枚举边来建图了。
dijkstra终于过了,代码真是丑的飞起,虽然比Floyd不知道快到哪里去了。
/* Pigonometry */ #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <utility> using namespace std; typedef pair<int, int> pii; const int maxn = 505, maxm = maxn * maxn, inf = 0x7fffffff, maxq = 10000; int n, m, head[maxn], head2[maxn], cur[maxn], cnt, cnt2, bg, ed, dis[2][maxn], depth[maxn], q[maxq]; priority_queue<pii, vector<pii>, greater<pii> > pq; struct _edge { int v, w, next; } g[maxm << 2], g2[maxm << 2]; struct _edge2 { int u, v, w, c; } mp[maxm]; inline int iread() { int f = 1, x = 0; char ch = getchar(); for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1; for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; return f * x; } inline void add(int u, int v, int w) { g[cnt] = (_edge){v, w, head[u]}; head[u] = cnt++; } inline void add2(int u, int v, int w) { g2[cnt2] = (_edge){v, w, head2[u]}; head2[u] = cnt2++; } inline int dijkstra(int f, int s, int d) { for(int i = 1; i <= n; i++) dis[f][i] = inf; dis[f][s] = 0; pq.push(pii(0, s)); while(!pq.empty()) { pii u = pq.top(); pq.pop(); int x = u.second; if(u.first > dis[f][x]) continue; for(int i = head[x]; ~i; i = g[i].next) if(dis[f][g[i].v] > dis[f][x] + g[i].w) { dis[f][g[i].v] = dis[f][x] + g[i].w; pq.push(pii(dis[f][g[i].v], g[i].v)); } } return dis[f][d]; } inline bool bfs() { for(int i = 0; i <= n; i++) depth[i] = -1; int h = 0, t = 0, u, i; depth[q[t++] = bg] = 1; while(h != t) for(i = head2[u = q[h++]]; ~i; i = g2[i].next) if(g2[i].w && !~depth[g2[i].v]) { depth[g2[i].v] = depth[u] + 1; if(g2[i].v == ed) return 1; q[t++] = g2[i].v; } return 0; } inline int dfs(int x, int flow) { if(x == ed) return flow; int left = flow; for(int i = cur[x]; ~i; i = g2[i].next) if(g2[i].w && depth[g2[i].v] == depth[x] + 1) { int tmp = dfs(g2[i].v, min(left, g2[i].w)); left -= tmp; g2[i].w -= tmp; g2[i ^ 1].w += tmp; if(tmp) cur[x] = i; if(!left) return flow; } if(left == flow) depth[x] = -1; return flow - left; } int main() { n = iread(); m = iread(); for(int i = 1; i <= n; i++) head[i] = head2[i] = -1; cnt = cnt2 = 0; for(int i = 1; i <= m; i++) { int u = iread(), v = iread(), w = iread(), c = iread(); add(u, v, w); add(v, u, w); mp[i] = (_edge2){u, v, w, c}; } printf("%d\n", dijkstra(0, 1, n)); dijkstra(1, n, 1); for(int i = 1; i <= m; i++) { if(dis[0][mp[i].u] + mp[i].w + dis[1][mp[i].v] == dis[0][n]) add2(mp[i].u, mp[i].v, mp[i].c), add2(mp[i].v, mp[i].u, 0); if(dis[0][mp[i].v] + mp[i].w + dis[1][mp[i].u] == dis[0][n]) add2(mp[i].v, mp[i].u, mp[i].c), add2(mp[i].u, mp[i].v, 0); } bg = 1; ed = n; int ans = 0; while(bfs()) { for(int i = 0; i <= n; i++) cur[i] = head2[i]; ans += dfs(bg, inf); } printf("%d\n", ans); return 0; }