然后最大流解决
这道题很值得体会体会
/* * this code is made by LinMeiChen * Problem: hdu 3416 * Type of Problem: 网络流 水题 * Thinking: * Feeling: */ #include#include using namespace std; typedef long long lld; typedef unsigned int ud; #define oo 0x3f3f3f3f #define eatline() char chch;while((chch=getchar())!='\n')continue; #define MemsetMax(a) memset(a,0x3f,sizeof a) #define MemsetZero(a) memset(a,0,sizeof a) #define MemsetMin(a) memset(a,-1,sizeof a) #define MemsetFalse(a) MemsetZero(a) #define PQ priority_queue #define Q queue #define maxn 1008 #define maxm 200008 struct Edge { int v, f, next; }E[maxm]; int level[maxn], h[maxn]; int stack[maxn]; int head[maxn],mark[maxn]; int dis1[maxn], dis2[maxn]; int map[maxn][maxn]; int c[maxn][maxn]; int q[maxm], front, rear; int tol, n, m; void Spfa(int s, int t, int dis[], int f) { fill(dis, dis + maxn, oo); memset(mark, 0, sizeof mark); front = rear = 0; q[rear++] = s; dis[s] = 0; mark[s] = 1; while (front<rear) { int u = q[front++]; mark[u] = 0; for (int i = 1; i <= n; i++) { int v = i; if (dis[v] > dis[u] + ( f==0 ? map[u][v]:map[v][u])) { dis[v] = dis[u] + ( f==0 ? map[u][v]:map[v][u]); if (!mark[v]) { q[rear++] = v; mark[v] = 1; } } } } } void add_edge(int u, int v, int f) { E[tol].v = v; E[tol].f = f; E[tol].next = head[u]; head[u] = tol++; E[tol].v = u; E[tol].f = 0; E[tol].next = head[v]; head[v] = tol++; } void Build(int s, int t) { memset(head, -1, sizeof head); tol = 0; for (int i = 1; i <= n;i++) for (int j = 1; j <= n; j++) { if (dis1[i] + dis2[j] + map[i][j] == dis1[t]) add_edge(i, j, c[i][j]); } } bool BFS(int s, int t) { memset(level, 0, sizeof level); level[s] = 1; front = rear = 0; q[rear++] = s; while (front < rear) { int u = q[front++]; if (u == t) return true; for (int i = head[u]; i != -1; i = E[i].next) { int v = E[i].v; if (!level[v] && E[i].f>0) { level[v] = level[u] + 1; q[rear++] = v; } } } return false; } int Dinic(int s, int t) { int maxflow = 0; while (BFS(s, t)) { memcpy(h, head, sizeof h); int top = 0, u = s; while (true) { if (u == t) { int minflow = oo, flag; for (int i = 0; i < top; i++) { if (minflow>E[stack[i]].f) { minflow = E[stack[i]].f; flag = i; } } for (int i = 0; i < top; i++) { E[stack[i]].f -= minflow; E[stack[i] ^ 1].f += minflow; } top = flag; maxflow += minflow; u = E[stack[top] ^ 1].v;//反向边的邻接点 } for (int i = h[u]; i != -1; i = h[u] = E[i].next) { int v = E[i].v; if (level[v] == level[u] + 1 && E[i].f) break; } if (h[u] != -1) { stack[top++] = h[u]; u = E[h[u]].v; } else { if (top == 0) break; level[u] = 0; u = E[stack[--top] ^ 1].v;//反向边的邻接点 } } } return maxflow; } int main() { int T; int s, t; scanf("%d", &T); while (T--) { memset(map, 0x3f, sizeof map); memset(c, 0, sizeof c); scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); if (map[u][v] > w) { map[u][v] = w; c[u][v] = 1; } else if (map[u][v] == w) c[u][v]++; } scanf("%d%d", &s, &t); Spfa(s, t, dis1,0); Spfa(t, s, dis2,1); Build(s, t); printf("%d\n", Dinic(s, t)); } return 0; }#include #include #include #include #include #include #include #include