HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)

67 篇文章 0 订阅
63 篇文章 0 订阅

Barricade

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


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
 

Recommend
wange2014
 

Statistic |  Submit |  Discuss |  Note


题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=5889

题目大意:

  N(N<=1000)个城市,你在城市1,敌人在城市N,敌人会选择从N到1的最短路进攻,你需要在某些边上放障碍来阻挡敌人进攻。

  总共有M(M<=1000)条无向边,连接两个城市,距离都为1,放置障碍的费用为wi。求最小费用。

题目思路:

  【BFS+最小割】

  首先因为每条边的距离都是1,所以先从N开始往1跑最短路,扩展所有距离d[u]<=d[1]的点,在最短路过程中,对于u->v的边,在新图上加一条v->u的容量为wi的边。

  这样从N跑完一次BFS之后建的新图是从1到N的一张最短路图。问题转化为求新图的最小割。从1到N开始跑最大流即可。



//
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-10)
#define J 10000
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define N 1004
#define M 10004
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
int nn,S,T;
int d[N],vd[N],last[N],last1[N];
bool u[N];
struct xxx
{
	int next,to,q;
}a[M<<1],b[M<<1];
void add(int x,int y,int z)
{
	a[++lll].next=last[x];
	a[lll].to=y;
	a[lll].q=z;
	last[x]=lll;
}
void link(int x,int y,int z)
{
	b[++cas].next=last1[x];
	b[cas].to=y;
	b[cas].q=z;
	last1[x]=cas;
}
int sap(int u,int f)
{
	int i,v,tt,asp=0,mix=nn-1;
	if(u==T)return f;
	for(i=last[u];i;i=a[i].next)
	{
		v=a[i].to;
		if(a[i].q>0)
		{
			if(d[u]==d[v]+1)
			{
				tt=sap(v,min(f-asp,a[i].q));
				asp+=tt;
				a[i].q-=tt;
				a[i^1].q+=tt;
				if(asp==f || d[S]==nn)
					return asp;
			}
			mix=min(mix,d[v]);
		}
	}
	if(asp!=0)return asp;
	if(!--vd[d[u]])d[S]=nn;
	else vd[d[u]=mix+1]++;
	return asp;
}
void bfs()
{
	int now,to,i;
	queue<int>q;
	mem(d,MAX);
	u[n]=1;q.push(n);d[n]=0;
	while(!q.empty())
	{
		now=q.front();q.pop();
		if(d[now]>=d[S])continue;
		for(i=last1[now];i;i=b[i].next)
		{
			to=b[i].to;
			if(d[now]+1>d[to])continue;
			d[to]=d[now]+1;
			add(now,to,0),add(to,now,b[i].q);
			if(!u[to])
			{
				u[to]=1;
				if(to!=S)q.push(to);
			}
		}
	}
	mem(d,0);
}
int main()
{
	#ifndef ONLINE_JUDGEW
	freopen("1.txt","r",stdin);
//	freopen("2.txt","w",stdout);
	#endif
	int i,j,k;
	int x,y,z,f;
//	init();
	for(scanf("%d",&cass);cass;cass--)
//	for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
//	while(~scanf("%s",s))
//	while(~scanf("%d",&n))
	{
		lll=cas=1;ans=0;
		mem(u,0);mem(vd,0);mem(last,0);mem(last1,0);
		scanf("%d%d",&n,&m);
		for(i=1;i<=m;i++)
		{
			scanf("%d%d%d",&x,&y,&z);
			link(x,y,z),link(y,x,z);
		}
		nn=n;
		S=1,T=n;
		vd[0]=nn;
		bfs();
		while(d[S]<nn)
		{
			f=sap(S,MAX);
			ans+=f;
		}
		printf("%d\n",ans);
	}
	return 0;
}
/*
//

//
*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值