Do not sincere non-interference。
Like that show, now starvae also take part in a show, but it take place between city A and B. Starvae is in city A and girls are in city B. Every time starvae can get to city B and make a data with a girl he likes. But there are two problems with it, one is starvae must get to B within least time, it's said that he must take a shortest path. Other is no road can be taken more than once. While the city starvae passed away can been taken more than once.
So, under a good RP, starvae may have many chances to get to city B. But he don't know how many chances at most he can make a data with the girl he likes . Could you help starvae?
Like that show, now starvae also take part in a show, but it take place between city A and B. Starvae is in city A and girls are in city B. Every time starvae can get to city B and make a data with a girl he likes. But there are two problems with it, one is starvae must get to B within least time, it's said that he must take a shortest path. Other is no road can be taken more than once. While the city starvae passed away can been taken more than once.
So, under a good RP, starvae may have many chances to get to city B. But he don't know how many chances at most he can make a data with the girl he likes . Could you help starvae?
For each case,there are two integer n and m in the first line ( 2<=n<=1000, 0<=m<=100000 ) ,n is the number of the city and m is the number of the roads.
Then follows m line ,each line have three integers a,b,c,(1<=a,b<=n,0<c<=1000)it means there is a road from a to b and it's distance is c, while there may have no road from b to a. There may have a road from a to a,but you can ignore it. If there are two roads from a to b, they are different.
At last is a line with two integer A and B(1<=A,B<=N,A!=B), means the number of city A and city B.
There may be some blank line between each case.OutputOutput a line with a integer, means the chances starvae can get at most.Sample Input
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 2Sample Output
2 1 1
题意:
一个人要从一个城市走到另一个城市,每次都走最短路,但是每次走的路不能是同一条,问一共有多少种不同的走法。
思路:
最大流问题。注意最后给的两个数字是起点和终点,一直没读懂。。。整体的思路是:从图中剔除不是最短路的边,然后把剩下的边(最短路上的边)建图,然后边权为1,求最大流,求出的值就是有多少种走法。那么如何找最短路呢?
先正向反向求最短路,获得起点到每点的最短距离d1[], 终点到每点的最短距离d2[],最短路minn。然后遍历每一条边,判断当d1[edges.from]+edges.dis+d2[edges.to]==minn时,将该边加入最大流的图中,容量为1,建完图后,以A为源点,B为汇点跑最大流即可。
代码:
#include<map>
#include<vector>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#define maxn 200000+5
//#define maxm 200000+5
#define PI acos(-1.0)
#define INF 1e9
using namespace std;
typedef long long ll;
int minn;
struct Edge{
int from,to,cap,flow;
ll dist;
Edge(){}
Edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){}
};
struct Dinic{
int n,m,s,t;
vector<Edge>edges;
vector<int>G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init(int m,int s,int t){
this->s=s;
this->t=t;
for(int i=0;i<=2*m;i++){
G[i].clear();
}
edges.clear();
}
void AddEdge(int from,int to,int cap){
edges.push_back((Edge){from,to,cap,0});
edges.push_back((Edge){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));
queue<int>Q;
Q.push(s);
d[s] = 0;
vis[s]=1;
while(!Q.empty()){
int x=Q.front();
Q.pop();
for(int i=0;i<(int)G[x].size();i++){
Edge &e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow){
vis[e.to]=1;
d[e.to]=d[x]+1;
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<(int )G[x].size();i++){
Edge& 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 Maxflow(){
int flow=0;
while(BFS()){
memset(cur,0,sizeof(cur));
flow+=DFS(s,INF);
}
return flow;
}
}DC;
struct HeapNode{
int d,u;
bool operator<(const HeapNode& rhs) const {
return d > rhs.d;
}
};
struct Dijkstra{
int n,m; ///点数和边数
vector<Edge>edges; ///边列表
vector<int>G[maxn];
bool done[maxn]; ///是否已永久编号
int d[maxn];///s到各个点的距离
int p[maxn];///最短路上的一条边
void init(int n,int m){
this->n=n;
for(int i=0;i<=2*m;i++) G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,ll dis){
Edge e;
e.from=from;
e.dist=dis;
e.to=to;
edges.push_back(e);
m=edges.size();
G[from].push_back(m-1);
}
void dijkstra(int s){///s到所有点的距离
priority_queue<HeapNode>Q;
for(int i=0;i<=n;i++) d[i]=INF;
d[s]=0;
memset(done,0,sizeof(done));
Q.push((HeapNode){0,s});
while(!Q.empty()){
HeapNode x=Q.top();
Q.pop();
int u=x.u;
if(done[u]) continue;
done[u]=true;
for(int i=0;i<(int )G[u].size();i++){
Edge &e=edges[G[u][i]];
if(d[e.to]>d[u]+e.dist){
d[e.to]=d[u]+e.dist;
// p[e.to]=G[u][i];
Q.push((HeapNode){d[e.to],e.to});
}
}
}
}
}dij,dij1;
int main()
{
int s,t;
int T;
int n,m;
scanf("%d",&T);
while(T--){
int u,v,d;
scanf("%d%d",&n,&m);
dij.init(n,m);
dij1.init(n,m);
for(int i=0;i<m;i++){
scanf("%d%d%d",&u,&v,&d);
dij.AddEdge(u,v,d);
dij1.AddEdge(v,u,d);
}
scanf("%d%d",&s,&t);
dij.dijkstra(s);
dij1.dijkstra(t);
minn=dij.d[t];
DC.init(m,s,t);
for(int i=0;i<m;i++){
if(dij.d[dij.edges[i].from] + dij1.d[dij.edges[i].to] + dij.edges[i].dist == minn)
DC.AddEdge(dij.edges[i].from, dij.edges[i].to, 1);
}
printf("%d\n",DC.Maxflow());
// printf("\n");
}
return 0;
}