HDU 5889 Barricade 最小割

Barricade

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


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(N1000)  and  M(M10000) .
The  i -the line of the next  M  lines describes the  i -th edge with three integers  u,v  and  w  where  0w1000  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


一个无向图,给你n个点m条边,每条边的长度相同。让你将图上的最短路封住。封住每条路有一个代价,求最小代价。


先bfs一遍求最短路,标记使用过的路径之后,直接上最小割。


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn=1005,maxk=10005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);  
int head[maxn],step[maxn],x[maxk],y[maxk],d[maxk];
int dist[maxn],current[maxn];
bool visit[maxn],mark[maxk*2];
int num; 

struct Edge {
	int from,to,flow,pre;
};
Edge edge[maxk*2];

void addedge(int from,int to,int flow) {
	edge[num]=(Edge){from,to,flow,head[from]};
	head[from]=num++;
	edge[num]=(Edge){to,from,0,head[to]};
	head[to]=num++;
}

bool bfs (int n) {
	queue<int> q;
	q.push(1);
	memset(dist,-1,sizeof(dist));
	dist[1]=0;
	while (!q.empty()) {
		int now=q.front();
		q.pop();
		for (int i=head[now];i!=-1;i=edge[i].pre) {
			int to=edge[i].to;
			if (dist[to]==-1&&edge[i].flow>0) {
				dist[to]=dist[now]+1;
				q.push(to);
			}
		}
	}
	return dist[n]!=-1;
}

int dfs(int now,int flow,int n) {
	int f;
	if (now==n) return flow;
	for (int i=current[now];i!=-1;i=edge[i].pre) {
		int to=edge[i].to;
		current[now]=i;
		if (dist[now]+1==dist[to]&&edge[i].flow>0&&
		(f=dfs(to,min(flow,edge[i].flow),n))) {
			edge[i].flow-=f;
			edge[i^1].flow+=f;
			return f;
		}
	}
	return 0;
}

int dinic(int n) {
	int sum=0,f;
	while (bfs(n)) {
		memcpy(current,head,sizeof(head));
		while (f=dfs(1,inf,n)) sum+=f;
	}
	return sum;
}

int main() {
	int cas;
	scanf("%d",&cas);
	while (cas--) {
		int n,m,i,j;
		num=0;
		memset(head,-1,sizeof(head));
		scanf("%d%d",&n,&m);
		for (i=1;i<=m;i++) {
			scanf("%d%d%d",&x[i],&y[i],&d[i]);
			addedge(x[i],y[i],d[i]);
		}
		mem0(visit);
		queue<int> q;
		q.push(1);
		step[1]=0;visit[1]=1;
		while (!q.empty()) {
			int now=q.front();
			q.pop();
			for (i=head[now];i!=-1;i=edge[i].pre) {
				int to=edge[i].to;
				if (!visit[to]) {
					visit[to]=1;
					step[to]=step[now]+1;
					q.push(to);
				}
			}
		}
		mem0(mark);
		for (i=1;i<=n;i++) {
			for (j=head[i];j!=-1;j=edge[j].pre) {
				int to=edge[j].to,from=edge[j].from;
				if (step[to]==step[from]+1) 
					mark[j]=1;
			}
		}
		num=0;memset(head,-1,sizeof(head));
		for (i=0;i<2*m;i+=2) {
			if (mark[i]) addedge(x[i/2+1],y[i/2+1],d[i/2+1]);
			if (mark[i+1]) addedge(y[i/2+1],x[i/2+1],d[i/2+1]);
		}
		int ans=dinic(n);
		printf("%d\n",ans);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值