Marriage Match IV HDU - 3416 (最短路 + 网络流)

本文介绍了一种基于Dijkstra算法和网络流的最短路径计数方法,用于解决从城市A到城市B的不重复路径数量问题。通过构建网络流图,确保每条边仅使用一次,并利用Dinic算法求解最大流,从而得出最多可能的数据匹配次数。

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?

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.

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

 

题意:找不重复的最短路的条数

思路:先dj求出最短路径长度,用d[i]表示起点到i点的最短路径长度,网络流建边,每条边容量为1,正边的长度为w,

反边为-w,网络流跑图的时候只有当d[to] = d[u] + w时,这连个点之间才可以转移,然后Dinic最大流就好了

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
int INF = 1e9;
int n, m;
struct Edge
{
	int to, next, w, cap;
} edge[maxn * 2];


int k, head[maxn];

void add(int a, int b, int c){
	edge[k].to = b;
	edge[k].w = c;
	edge[k].cap = 1;
	edge[k].next = head[a];
	head[a] = k++;

	edge[k].to = a;
	edge[k].w = -c;
	edge[k].cap = 0;
	edge[k].next = head[b];
	head[b] = k++;
}

struct qnode
{
	int u, c;
	bool operator < (const qnode &r) const{
		return c > r.c;
	}
};

int d[maxn];
priority_queue<qnode> que;
void dj(int s){
	while(que.size()){
		que.pop();
	}
	que.push(qnode{s, 0});
	for(int i = 1; i <= n; i++){
		d[i] = INF;
	}
	d[s] = 0;
	while(que.size()){
		qnode tmp = que.top();
		que.pop();
		int u = tmp.u;
		for(int i = head[u] ; i != -1; i = edge[i].next){
			if(edge[i].w < 0){
				continue;
			}
			int to = edge[i].to;
			int w = edge[i].w;
			if(d[to] > d[u] + w){
				d[to] = d[u] + w;
				que.push(qnode{to, d[to]});
			}
		}
	}
}


int dis[maxn];
int cur[maxn];
int bfs(int s, int t){
	queue<int> que;
	for(int i = 1; i <= n; i++){
		dis[i] = 0;
	}
	que.push(s);
	dis[s] = 1;
	while(que.size()){
		int u = que.front();
		que.pop();
		for(int i = head[u]; i != -1; i = edge[i].next){
			int to = edge[i].to;
			int cap = edge[i].cap;
			if(!dis[to] && cap && d[to] == d[u] + edge[i].w){
				dis[to] = dis[u] + 1;
				if(to == t){
					return 1;
				}
				que.push(to);
			}
		}
	}
	return 0;
}

int dfs(int u, int maxf, int t){
	if(u == t){
		return maxf;
	}
	int ret = 0;
	for(int i = cur[u]; i != -1; i = edge[i].next){
		cur[u] = i;
		int to = edge[i].to;
		int cap = edge[i].cap;
		if(dis[to] == dis[u] + 1 && cap && d[to] == d[u] + edge[i].w){
			int mi = min(maxf - ret, cap);
			cap = dfs(to, mi, t);
			edge[i].cap -= cap;
			edge[i ^ 1].cap += cap;
			ret += cap;
			if(ret == maxf){
				return ret;
			}
		}
	}
	return ret;
}

int Dinic(int s, int t){
	int ans = 0;
	while(bfs(s, t)){
		for(int i = 1; i <= n; i++){
			cur[i] = head[i];
		}
		ans += dfs(s, INF, t);
	}
	return ans;
}
int main() {
	int tt;
	scanf("%d", &tt);
	while(tt--){
		k = 0;
		scanf("%d%d", &n, &m);
		for(int i = 1; i <= n; i++){
			head[i] = -1;
		}
		for(int i = 1; i <= m; i++){
			int a, b, c;
			scanf("%d%d%d", &a, &b, &c);
			if(a == b){ //不考虑自环,放进去也不会wa
				continue;
			}
			add(a, b, c);
		}

		int s, t;
		scanf("%d%d", &s, &t);
		dj(s);
		printf("%d\n", Dinic(s, t));
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值