题意:T组测试数据,给你N个点M条单向边,再给你起点A,终点B, 问你A~B的最短路最多有多少条(每条边只能走一次)
思路:先求出在最短路上的所有的边,然后以这些边建图,容量均为1,跑A~B的最大流就是答案。
正向建边跑一边最短路得到一个以A为源点最短路(dis1),反向建边再跑一次得到一个以B为起点的最短路(dis2),最短路径长度为dis1[B](dis1[B]==dis2[A])。如果对于边权为w的边u~v,如果dis1[u] + w + dis2[v] == dis1[B],那么这条边一定在最短路上。遍历所有的边,以所有最短路上的边建图,跑一波A~B的最大流就OK。
代码较长,变量名较多,记得初始化,别写出BUG就好。我第一次自己手敲这么长的代码,结果调试了几分钟就AC了,虽然题比较简单,而且两个算法都是模板,但是还是很开心的,哈哈哈!
Dijkstra + Dinic:
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e3 + 5;
const int MAXM = 1e5 + 5;
const int INF = 0x3f3f3f3f;
//Dijkstra
struct Edge1
{
int from, to, dist;
Edge1(int u, int v, int w): from(u), to(v), dist(w) {}
};
struct Dijkstra
{
int n, m;
vector<Edge1> edges;
vector<int> G[MAXN];
int d[MAXN];
int vis[MAXN];
int pre[MAXN];
void init(int n)
{
this->n = n;
edges.clear();
for (int i = 0; i <= n; i++) G[i].clear();
}
void add_edge(int from, int to, int dist)
{
edges.push_back(Edge1(from, to, dist));
m = edges.size();
G[from].push_back(m - 1);
}
struct HeapNode
{
int from, dist;
bool operator < (const HeapNode& rhs) const
{
return rhs.dist < dist;
}
HeapNode(int u, int w): from(u), dist(w) {}
};
void dijkstra(int s)
{
for (int i = 0; i <= n; i++) d[i] = INF;
memset(vis, 0, sizeof(vis));
d[s] = 0;
priority_queue<HeapNode> Q;
Q.push(HeapNode(s, 0));
while (!Q.empty())
{
HeapNode x = Q.top(); Q.pop();
int u = x.from;
if (vis[u]) continue;
vis[u] = true;
for (int i = 0; i < G[u].size(); i++)
{
Edge1& e = edges[G[u][i]];
if (!vis[e.to] && d[e.to] > d[u] + e.dist)
{
d[e.to] = d[u] + e.dist;
pre[e.to] = G[u][i];
Q.push(HeapNode(e.to, d[e.to]));
}
}
}
}
};
//Dinic
struct Edge2
{
int from, to, cap, flow;
Edge2(int u, int v, int c, int f): from(u), to(v), cap(c), flow(f) {}
};
struct Dinic
{
int n, m, s, t;
vector<Edge2> edges;
vector<int> G[MAXN];
int d[MAXN];
int vis[MAXN];
int cur[MAXN];
void init(int n)
{
this->n = n;
edges.clear();
for (int i = 0; i <= n; i++) G[i].clear();
}
void add_edge(int from, int to, int cap)
{
edges.push_back(Edge2(from, to, cap, 0));
edges.push_back(Edge2(to, from, 0, 0));
m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool BFS()
{
memset(vis, 0, sizeof(vis));
d[s] = 0; vis[s] = true;
queue<int> Q;
Q.push(s);
while (!Q.empty())
{
int u = Q.front(); Q.pop();
for (int i = 0; i < G[u].size(); i++)
{
Edge2& e = edges[G[u][i]];
if (!vis[e.to] && e.cap > e.flow)
{
d[e.to] = d[u] + 1;
vis[e.to] = true;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int a)
{
if (x == t || a == 0) return a;
int flow = 0, f;
for (int& i = cur[x]; i < G[x].size(); i++)
{
Edge2& e = edges[G[x][i]];
if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0)
{
e.flow += f;
edges[G[x][i]^1].flow -= f;
flow += f;
a -= f;
if (a == 0) break;
}
}
return flow;
}
int max_flow(int s, int t)
{
this->s = s; this->t = t;
int flow = 0;
while (BFS())
{
memset(cur, 0, sizeof(cur));
flow += DFS(s, INF);
}
return flow;
}
};
struct Edge
{
int from, to, dist;
}edge[MAXM];
int main()
{
int T, n, m, A, B;
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &m);
Dijkstra solve1, solve2;//正反向最短路
solve1.init(n); solve2.init(n);
for (int i = 0; i < m; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
edge[i].from = u; edge[i].to = v; edge[i].dist= w;
solve1.add_edge(u, v, w);
solve2.add_edge(v, u, w);
}
scanf("%d%d", &A, &B);
solve1.dijkstra(A);
solve2.dijkstra(B);
int ans = solve1.d[B];//最短路径长度
Dinic solve3;
solve3.init(n);
for (int i = 0; i < m; i++)
{
Edge e = edge[i];
if (solve1.d[e.from] + e.dist + solve2.d[e.to] == ans)
{
solve3.add_edge(e.from, e.to, 1);
}
}
ans = solve3.max_flow(A, B);//A~B的最大流
printf("%d\n", ans);
}
return 0;
}
/*
3
7 8
1 2 1
1 3 1
2 4 1
3 4 1
4 5 1
4 6 1
5 7 1
6 7 1
1 7
6 7
1 2 1
2 3 1
1 3 3
3 4 1
3 5 1
4 6 1
5 6 1
1 6
2 2
1 2 1
1 2 2
1 2
*/