题目链接:传送门
Marriage Match IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4033 Accepted Submission(s): 1225
Problem Description
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?
Input
The first line is an integer T indicating the case number.(1<=T<=65)
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.
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.
Output
Output 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 2
Sample Output
2 1 1
题目大意:从起点到终点有多少条不同(边都不重复)的最短路。
解题思路:找到图中最短路上的边uv,再建一个新图,从u点到v点建条容量c=1的边,最后在新图上跑个最大流
#include <cstdio>
#include <algorithm>
#include <queue>
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1300;
const int M = 100900;
struct edge{
int node,len,flag;
edge*next;
}m_edge[M*2];
edge*head[N];
int Ecnt,dist[N],vis[N];
//边的结构
struct edge_t{
int node;
int c;//c为容量
edge_t* next;
edge_t* redge;//指向反向边
}Edge[M*2];
//图的邻接表
edge_t* Ver[N];
int ECnt;
void init()
{
Ecnt = ECnt = 0;
fill( head , head+N , (edge*)0 );
fill( Ver , Ver+N , (edge_t*)0 );
}
void MK( int a , int b , int c , int flag )
{
m_edge[Ecnt].node = b;
m_edge[Ecnt].len = c;
m_edge[Ecnt].next = head[a];
m_edge[Ecnt].flag = flag;
head[a] = m_edge+Ecnt++;
}
//生成双向边
void mkEdge(int a,int b,int c){
int t1 = ECnt++;
int t2 = ECnt++;
Edge[t1].node = b;
Edge[t1].c = c;
Edge[t1].next = Ver[a];
Edge[t1].redge = Edge + t2;
Ver[a] = Edge + t1;
Edge[t2].node = a;
Edge[t2].c = 0;
Edge[t2].next = Ver[b];
Edge[t2].redge = Edge + t1;
Ver[b] = Edge + t2;
}
void spfa( int u )
{
fill( dist , dist+N , INT_MAX );
fill( vis , vis+N , 0 );
queue<int>point;
point.push(u);
vis[u] = 1;
dist[u] = 0;
while( !point.empty() ){
int s = point.front();
point.pop();
vis[s] = 0;
for( edge*p = head[s] ; p ; p = p->next ){
int t = p->node;
if( dist[t] > dist[s]+p->len ){
dist[t] = dist[s]+p->len;
if( !vis[t] ){
point.push(t);
vis[t] = 1;
}
}
}
}
}
int L[N];//层次图
//建立残留网络从源s到汇t的层次图
bool bfs(int s,int t){
fill(L,L+N,-1);
queue<int>q;
q.push(s);
L[s] = 0;
while( !q.empty() ){
int u = q.front();
q.pop();
//寻找还有残量的边
for(edge_t*p=Ver[u];p;p=p->next){
if ( p->c <= 0 ) continue;
int v = p->node;
if ( -1 != L[v] ) continue;
q.push(v);
L[v] = L[u] + 1;
}
}
return -1 != L[t];
}
//在层次图上搜索增广路径,本质上就是搜索可以增广的流量
//这个流量是各层之间流量的最小值
//u为当前节点,cf为当前层的最小流,t为汇点
int dfs(int u,int e,int cf){
if ( u == e ) return cf;
int tf = 0; //tf记录u往下一层的总可行流量
for(edge_t*p=Ver[u];p;p=p->next){
int v = p->node;
int c = p->c;
if ( L[u] + 1 == L[v] && c > 0 && cf > tf ){
int f = dfs(v,e,min(c,cf-tf));
if ( 0 == f ) continue;
p->c -= f;//正向边减去可行流量
p->redge->c += f;//反向边加上
tf += f;
}
}
if ( 0 == tf ) L[u] = -1;//修改层次图
return tf;
}
//Dinic算法,s为源,t为汇
int Dinic(int s,int t){
int ret = 0;
while( bfs(s,t) ){//第一步建立分层图
int ans;
//第二步在分层图上查找一条增广路径的可行流量
while( ans = dfs(s,t,INT_MAX) )
ret += ans;
}
return ret;
}
void Build( int u )
{
//vis[u] = 1;
for( edge*p = head[u] ; p ; p = p->next ){
int v = p->node;
if( dist[u] + p->len != dist[v] || p->flag ) continue;
p->flag = 1;
mkEdge(u,v,1);
Build( v );
}
}
int main()
{
int T;
scanf("%d",&T);
while( T-- ){
init();
int n,m,s,t;
scanf("%d%d",&n,&m);
int a,b,c;
for( int i = 0 ; i < m ; ++i ){
scanf("%d%d%d",&a,&b,&c);
MK(a,b,c,0);
}
scanf("%d%d",&s,&t);
spfa(s);
//fill( vis , vis+N , 0 );
Build( s );
printf("%d\n",Dinic(s,t));
}
return 0;
}