您的位置:首页 > 理论基础 > 计算机网络

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

2016-09-22 22:22 639 查看

Barricade

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


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each test cases, output the minimum wood cost.

[align=left]Sample Input[/align]

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

[align=left]Sample Output[/align]

4

[align=left]Source[/align]
2016 ACM/ICPC Asia Regional Qingdao Online

[align=left]Recommend[/align]
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
,vd
,last
,last1
;
bool u
;
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
=1;q.push(n);d
=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;
}
/*
//

//
*/


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐