题意:
给出一个有向图,求最短路的个数,不同的最短路不能共用一条边。
思路:
刚开始想到的是把费用流改造一下,每次找到一条增广路就把这条路径满流(只能走一次),记录下第一次的耗费(最少),后面再找到增广路的时候检查耗费是否等于第一次的耗费,如果大于,则结束,如果等于,则计数。这个方法跑了太多次最短路,超时。
还有一种方法是,先跑最短路,找出最短路的路径,在此基础上建立网络跑最大流。
找最短路的路径:可能不止一条,方法是正向和反向分别求一次单源最短路,然后进行判断,对于任意一条边(i,j),如果(s, i) + (i , j) + (t , j) = (s, t)即这条边在最短路的路径上。(a,b)指a到b的最短距离。
超时代码:
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 1000+5;
using namespace std;
int n,m;
int d[maxn], pre[maxn];
bool inq[maxn];
// 图
struct Edge{
int u, v, cap, flow, cost;
Edge(int a, int b, int c, int d, int e):u(a),v(b),cap(c),flow(d),cost(e){}
};
vector<Edge> edges;
vector<int> G[maxn];
void init(int a){
for(int i = 0; i < a; ++i) G[i].clear();
edges.clear();
}
void addEdge(int u, int v, int cap, int cost){
edges.push_back(Edge(u,v,cap,0,cost));
int m = edges.size();
G[u].push_back(m-1);
}
bool Bellman(int s, int t, int& flow, int& pre_cost){
for(int i = 0; i <= n; ++i) d[i] = INF;
memset(inq, 0, sizeof(inq));
d[s] = 0;
queue<int> Q;
Q.push(s);
while(!Q.empty()){
int x = Q.front(); Q.pop();
inq[x] = 0;
for(int i = 0; i < G[x].size(); ++i){
Edge e = edges[G[x][i]];
if(d[e.v] > d[x] + e.cost&&e.cap > e.flow){
d[e.v] = d[x] + e.cost;
pre[e.v] = G[x][i];
if(!inq[e.v]){Q.push(e.v); inq[e.v] = 1;}
}
}
}
if(pre_cost == -1) pre_cost = d[t];
if(d[t] == INF||d[t] > pre_cost) return false;
//printf("pre_cost = %d, now = %d\n", pre_cost, d[t]);
flow += 1;
for(int u = t; u != s; u = edges[pre[u]].u){
edges[pre[u]].flow = 1;
}
return true;
}
int solve(int s, int t){
int flow = 0, pre_cost = -1;
while(Bellman(s, t, flow, pre_cost)) ;
return flow;
}
int main()
{
freopen("in.txt","r",stdin);
int T; scanf("%d",&T);
while(T--){
int s,t;
scanf("%d%d",&n,&m);
init(n+1);
for(int i = 0; i < m; ++i){
int a,b,c; scanf("%d%d%d",&a,&b,&c);
if(a == b) continue;
addEdge(a, b, 1, c);
}
scanf("%d%d",&s,&t);
int ans = solve(s, t);
printf("%d\n", ans);
}
fclose(stdin);
return 0;
}
AC代码:
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 1000+5;
using namespace std;
int n,m;
int d1[maxn], d2[maxn];// 正向和反向距离
bool inq[maxn];
struct Edge1{
int v, w;
Edge1(int a = 0, int b = 0):v(a),w(b){}
};
vector<Edge1> G1[maxn], G2[maxn];
void Bellman1(int s, int t){
for(int i = 0; i <= n; ++i) d1[i] = INF;
memset(inq, 0, sizeof(inq));
d1[s] = 0;
queue<int> Q;
Q.push(s);
while(!Q.empty()){
int x = Q.front(); Q.pop();
inq[x] = 0;
for(int i = 0; i < G1[x].size(); ++i){
int v = G1[x][i].v, w = G1[x][i].w;
if(d1[v] > d1[x] + w){
d1[v] = d1[x] + w;
if(!inq[v]){Q.push(v); inq[v] = 1;}
}
}
}
}
void Bellman2(int s, int t){
for(int i = 0; i <= n; ++i) d2[i] = INF;
memset(inq, 0, sizeof(inq));
d2[s] = 0;
queue<int> Q;
Q.push(s);
while(!Q.empty()){
int x = Q.front(); Q.pop();
inq[x] = 0;
for(int i = 0; i < G2[x].size(); ++i){
int v = G2[x][i].v, w = G2[x][i].w;
if(d2[v] > d2[x] + w){
d2[v] = d2[x] + w;
if(!inq[v]){Q.push(v); inq[v] = 1;}
}
}
}
}
// 最大流 算法
// 图
struct Edge{
int u, v, cap, flow;
Edge(int a, int b, int c, int d):u(a),v(b),cap(c),flow(d){}
};
vector<Edge> edges;
vector<int> G[maxn];
int dis[maxn]; // 分层的编号
int cur[maxn]; // 当前弧,重要优化!!!
void init(int a){
for(int i = 0; i < a; ++i) G[i].clear();
edges.clear();
}
void addEdge(int u, int v, int cap){
edges.push_back(Edge(u,v,cap,0));
edges.push_back(Edge(v,u,0,0)); // 反向弧
int m = edges.size();
G[u].push_back(m-2);
G[v].push_back(m-1);
}
// 分层
bool bfs(int s, int t){
memset(dis, -1, sizeof(dis));
dis[s] = 0;
queue<int> Q;
Q.push(s);
while(!Q.empty()){
int x = Q.front(); Q.pop();
for(int i = 0; i < G[x].size(); ++i){
Edge e = edges[G[x][i]];
if(dis[e.v] == -1&&e.cap > e.flow){
dis[e.v] = dis[x] + 1;
Q.push(e.v);
}
}
}
return dis[t] != -1;
}
int dfs(int s, int t, int cur_flow){
if(s == t||cur_flow == 0) return cur_flow;
int ans = 0;
for(int& i = cur[s]; i < G[s].size(); ++i){
int c = G[s][i];
Edge e = edges[c];
if(dis[e.v] == dis[s] + 1&&e.cap > e.flow){
int a2 = min(cur_flow, e.cap-e.flow);
int w = dfs(e.v, t, a2);
edges[c].flow += w;
edges[c^1].flow -= w;
cur_flow -= w;
ans += w;
if(cur_flow <= 0) break;
}
}
return ans;
}
int Dinic(int s, int t){
int ans = 0;
while(bfs(s,t)){
memset(cur, 0, sizeof(cur));
ans += dfs(s,t,INF);
}
return ans;
}
int main()
{
freopen("in.txt","r",stdin);
int T; scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
init(n+1);
for(int i = 0; i <= n; ++i){ G1[i].clear(); G2[i].clear();}
for(int i = 0; i < m; ++i){
int a,b,c; scanf("%d%d%d",&a,&b,&c);
if(a == b) continue;
G1[a].push_back(Edge1(b,c)); // 正向图
G2[b].push_back(Edge1(a,c)); // 反向图
}
int s,t; scanf("%d%d",&s,&t);
// 建立网络
Bellman1(s, t);
Bellman2(t, s);
for(int i = 1; i <= n; ++i){
for(int j = 0; j < G1[i].size(); ++j){
int v = G1[i][j].v, w = G1[i][j].w;
if(d1[i] + w + d2[v] == d1[t]) addEdge(i, v, 1);
}
}
//printf("dis = %d,%d\n",d1[2],d2[2]);
printf("%d\n", Dinic(s, t));
}
fclose(stdin);
return 0;
}