点击查看原题
题目大意:从A地到B地有几条路径不重复的最短路
分析:题意很简单,首先先跑两遍最短路 找到A地到各点的最短路和B到各点的最短路,然后找到属于最短路里的路径进行最大流的建图 disA[u]+disB[v]+w=disA[B];
(A点到当前边的起点的距离+B到当前边终点的距离+当前边权值 判断是否等于最短路距离)
AC代码
//最大流加最短路
#include<stdio.h>
#include<string.h>
#include<queue>
#include<set>
#include<vector>
#define INF 100000000
using namespace std;
const int N=1010;
const int M=100005;
int n,m;
struct node
{
int u,v,w;
int next;
node(){}
node(int uu,int vv,int ww,int nn)
{
u=uu;v=vv;w=ww;next=nn;
}
};
struct stone
{
int i,w;
stone(){}
stone(int ii,int ww)
{
i=ii;w=ww;
}
bool operator <(const stone & a) const
{
return a.w<w;
}
};
int st,ed;
node q1[M],q2[M];
int vis[N];
int dis[2][N];
int first1[N];
int first2[N];
void spfa(int s,int t,int flag,int *head,node edge[])
{
for(int i=1;i<=n;i++)
{
dis[flag][i] = INF;
vis[i] = false;
}
dis[flag][s] = 0;
queue<int>q;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int k=head[u];k!=-1;k=edge[k].next)
{
int v = edge[k].v,w=edge[k].w;
if(dis[flag][v]>dis[flag][u]+w)
{
dis[flag][v] = dis[flag][u]+w;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
}
struct tree
{
int from,to,cap,flow;
tree(){}
tree(int ff,int tt,int cc,int ww)
{
from=ff,to=tt,cap=cc,flow=ww;
}
};
vector <tree> G;
vector<int>v[200005];
int cur[N];
int d[N];
void addtree(int from,int to,int cap)
{
G.push_back(tree(from,to,cap,0));
G.push_back(tree(to,from,0,0));
int m=G.size();
v[from].push_back(m-2);
v[to].push_back(m-1);
}
bool bfs()
{
memset(d,-1,sizeof(d));
queue<int>que;
que.push(st);
d[st]=0;
while(!que.empty())
{
int p=que.front();
que.pop();
if(p==ed) return true;
for(int i=0;i<v[p].size();i++)
{
tree & e=G[v[p][i]];
if(d[e.to]==-1&&e.flow<e.cap)
{
d[e.to]=d[p]+1;
que.push(e.to);
}
}
}
return false;
}
int dfs(int x,int a)
{
if(x==ed||a==0)
return a;
int flow=0,f;
for(int &i=cur[x];i<v[x].size();i++)
{
tree &e=G[v[x][i]];
if(d[e.to]=d[x]+1&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
{
e.flow+=f;
G[v[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(a==0)
break;
}
}
if(flow==0) d[x]=-2;
return flow;
}
int maxflow()
{
int flow=0;
while(bfs())
{
memset(cur,0,sizeof(cur));
flow+=dfs(st,INF);
}
return flow;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(first1,-1,sizeof(first1));
memset(first2,-1,sizeof(first2));
for(int i=0;i<=2*m;i++)
v[i].clear();
G.clear();
for(int i=0;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
if(u==v) continue;
q1[i].u=u;q1[i].v=v;q1[i].w=w;
q1[i].next=first1[u];
first1[u]=i;
q2[i].u=v;q2[i].v=u;q2[i].w=w;
q2[i].next=first2[v];
first2[v]=i;
}
scanf("%d%d",&st,&ed);
spfa(st,ed,0,first1,q1);
spfa(ed,st,1,first2,q2);
int aim=dis[0][ed];
if(aim==INF)
printf("0\n");
else
{
for(int i=1;i<=n;i++)
{
for(int j=first1[i];j!=-1;j=q1[j].next)
{
if(dis[0][i]+dis[1][q1[j].v]+q1[j].w==aim)
{
addtree(i,q1[j].v,1);
}
}
}
printf("%d\n",maxflow());
}
}
}