HDU 5889 Barricade (最短路 + 最小割)

Barricade

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2224    Accepted Submission(s): 652

Problem Description

The empire is under attack again. The general of empire is planning to defend his castle. The land can be seen as N towns and M roads, and each road has the same length and connects two towns. The town numbered 1 is where general's castle is located, and the town numbered N is where the enemies are staying. The general supposes that the enemies would choose a shortest path. He knows his army is not ready to fight and he needs more time. Consequently he decides to put some barricades on some roads to slow down his enemies. Now, he asks you to find a way to set these barricades to make sure the enemies would meet at least one of them. Moreover, the barricade on the i-th road requires wi units of wood. Because of lacking resources, you need to use as less wood as possible.

Input

The first line of input contains an integer t, then t test cases follow.
For each test case, in the first line there are two integers N(N≤1000) and M(M≤10000).
The i-the line of the next M lines describes the i-th edge with three integers u,v and w where 0≤w≤1000 denoting an edge between u and v of barricade cost w.

Output

For each test cases, output the minimum wood cost.

Sample Input

1 4 4 1 2 1 2 4 2 3 1 3 4 3 4

Sample Output

4

Source

2016 ACM/ICPC Asia Regional Qingdao Online

思路:跑个SPFA把所有的最短路找出来,如果dis[u]=dis[v]+1,说明uv是最短路,剩下就是裸的最小割了,Dinic和SPFA用的lrj的模板

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4010;
const int MAXM = 40010;
const int INF = 0x3f3f3f3f;
struct Edge1
{
	int from,to,cap,flow;
};
struct Dinic
{
	int n,m,s,t;
	vector<Edge1> edges;
	vector<int> G[MAXN];
	bool vis[MAXN];
	int d[MAXN];
	int cur[MAXN];
	void init(int n) 
	{
		this -> n = n;
		for(int i = 0; i <= n + 1; i++){
			G[i].clear();
		}
		edges.clear();
	}
	void AddEdge(int from,int to,int cap) 
	{
		edges.push_back((Edge1){from,to,cap,0});	
		edges.push_back((Edge1){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 < G[x].size(); i++) {
				Edge1& 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 < G[x].size(); i++) {
			Edge1& 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 s,int t) {
		this -> s = s,this -> t = t;
		int flow = 0;
		while(BFS()) {
			memset(cur,0,sizeof(cur));
			flow += DFS(s,INF);
		}
		return flow;
	}
}din;
struct Edge2{
	int from,to,dist,w; 
}; 
struct SPFA{
	
	int n,m;
	vector<Edge2> edges;
	vector<int> G[MAXN];
	bool inq[MAXN];
	int d[MAXN];
	int p[MAXN];
	int cnt[MAXN];
	
	void init(int n) 
	{
		this -> n = n;
		for(int i = 0; i <= n + 1; i++){
			G[i].clear();
		}
		edges.clear();
	}
	void AddEdge(int from,int to,int dist,int w)
	{
		edges.push_back((Edge2){from,to,dist,w});
		m = edges.size();
		G[from].push_back(m - 1);
	}
	bool spfa(int s)
	{
		queue<int> Q;
		for(int i = 0; i <= n; i++) {
			d[i] = INF;
			cnt[i] = 0;
			inq[i] = 0;
		}
		d[s] = 0;
		Q.push(s);
		inq[s] = true;	
		while(!Q.empty()) {
			int u = Q.front();
			Q.pop();		
			inq[u] = false;
			for(int i = 0; i < G[u].size(); i++) {
				Edge2& e = edges[G[u][i]]; 
				if(d[u] < INF && d[e.to] > d[u] + e.dist) {
					d[e.to] = d[u] + e.dist;
					p[e.to] = G[u][i];		
					if(!inq[e.to]) {
						Q.push(e.to);
						inq[e.to] = true;
						if(++cnt[e.to] > n) {
							return  false;
						}
					}
				}
			}
		}
		return true;
	}
}sp; 
int main(void)
{
	int T,n,m;
	int u,v,w;
	scanf("%d",&T);
	while(T--) {
		scanf("%d %d",&n,&m);
		sp.init(n);
		while(m--) {
			scanf("%d %d %d",&u,&v,&w);
			sp.AddEdge(u,v,1,w);
			sp.AddEdge(v,u,1,w);
		}
		sp.spfa(1);
		din.init(n);
		for(int i = 1; i <= n; i++) {
			for(int j = 0; j < sp.G[i].size(); j++) {
				Edge2 e = sp.edges[sp.G[i][j]];
				if(sp.d[e.to] == sp.d[i] + 1) {
					din.AddEdge(i,e.to,e.w);
				}
			}
 		}
 		printf("%d\n",din.Maxflow(1,n));
	}
	return 0;
}
/*
1
4 4
1 2 1
2 4 2
3 1 3
4 3 4
*/

猜你喜欢

转载自blog.csdn.net/GYH0730/article/details/81783445
今日推荐