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(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
 
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<<],b[M<<];
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=,mix=nn-;
if(u==T)return f;
for(i=last[u];i;i=a[i].next)
{
v=a[i].to;
if(a[i].q>)
{
if(d[u]==d[v]+)
{
tt=sap(v,min(f-asp,a[i].q));
asp+=tt;
a[i].q-=tt;
a[i^].q+=tt;
if(asp==f || d[S]==nn)
return asp;
}
mix=min(mix,d[v]);
}
}
if(asp!=)return asp;
if(!--vd[d[u]])d[S]=nn;
else vd[d[u]=mix+]++;
return asp;
}
void bfs()
{
int now,to,i;
queue<int>q;
mem(d,MAX);
u[n]=;q.push(n);d[n]=;
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]+>d[to])continue;
d[to]=d[now]+;
add(now,to,),add(to,now,b[i].q);
if(!u[to])
{
u[to]=;
if(to!=S)q.push(to);
}
}
}
mem(d,);
}
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=;ans=;
mem(u,);mem(vd,);mem(last,);mem(last1,);
scanf("%d%d",&n,&m);
for(i=;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
link(x,y,z),link(y,x,z);
}
nn=n;
S=,T=n;
vd[]=nn;
bfs();
while(d[S]<nn)
{
f=sap(S,MAX);
ans+=f;
}
printf("%d\n",ans);
}
return ;
}
/*
// //
*/

HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)的更多相关文章

  1. 2016 ACM/ICPC Asia Regional Qingdao Online(2016ACM青岛网络赛部分题解)

    2016 ACM/ICPC Asia Regional Qingdao Online(部分题解) 5878---I Count Two Three http://acm.hdu.edu.cn/show ...

  2. 2016 ACM/ICPC Asia Regional Qingdao Online 1001/HDU5878 打表二分

    I Count Two Three Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  4. 【2016 ACM/ICPC Asia Regional Qingdao Online】

    [ HDU 5878 ] I Count Two Three 考虑极端,1e9就是2的30次方,3的17次方,5的12次方,7的10次方. 而且,不超过1e9的乘积不过5000多个,于是预处理出来,然 ...

  5. 2016 ACM/ICPC Asia Regional Qingdao Online

    吐槽: 群O的不是很舒服 不知道自己应该干嘛 怎样才能在团队中充分发挥自己价值 一点都不想写题 理想中的情况是想题丢给别人写 但明显滞后 一道题拖沓很久 中途出岔子又返回来搞 最放心的是微软微软妹可以 ...

  6. Hdu OJ 5884-Sort (2016 ACM/ICPC Asia Regional Qingdao Online)(二分+优化哈夫曼)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 题目大意:有n个有序的序列,对于第i个序列有ai个元素. 现在有一个程序每次能够归并k个序列, ...

  7. hdu 5878 I Count Two Three (2016 ACM/ICPC Asia Regional Qingdao Online 1001)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5878 题目大意: 给出一个数n ,求一个数X, X>=n. X 满足一个条件 X= 2^a*3^ ...

  8. 2016 ACM/ICPC Asia Regional Qingdao Online HDU5889

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5889 解法:http://blog.csdn.net/u013532224/article/details ...

  9. 2016 ACM/ICPC Asia Regional Qingdao Online HDU5883

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5883 解法:先判断是不是欧拉路,然后枚举 #pragma comment(linker, "/S ...

随机推荐

  1. 第一章 Android体系与系统架构

    1. Dalvik 和 ART(Android Runtime) 在Dalvik中应用好比是一辆可折叠的自行车,平时是折叠的,只有骑的时候,才需要组装起来用.在ART中应用好比是一辆组装好了的自行车, ...

  2. 【转】纯 CSS 实现高度与宽度成比例的效果

    先来演示页面:Demo; 转的内容: 最近在做一个产品列表页面,布局如右图所示.页面中有若干个 item,其中每个 item 都向左浮动,并包含在自适应浏览器窗口宽度的父元素中. item 元素的 C ...

  3. asp.net中ashx文件如何调用session

    如果你要保证数据的安全性,你可以在ashx中使用session验证.如:你的index.aspx中使用jquery回调ashx数据,那么在index.aspx page_load时session[&q ...

  4. sp_addlinkedserver的一些操作

    sp_addlinkedserver 创建一个链接的服务器,使其允许对分布式的.针对 OLE DB 数据源的异类查询进行访问.在使用 sp_addlinkedserver 创建链接的服务器之后,此服务 ...

  5. [转帖]音响及DarBee

    红外与蓝牙的差别 1.距离 红外:对准.直接.1—2米,单对单 红外线可以用你的手机摄像头看到  蓝牙:10米左右,可加强信号,可以绕弯,可以不对准,可以不在同一间房间,链接最大数目可达7个,同时区分 ...

  6. Nginx和Apache共存环境下apache获得真实IP

    自从Nginx出现以后,我们都喜欢让 Nginx 跑在前方处理静态文件,然后通过 proxy 把动态请求过滤给 apache.这么有个问题,跑在后方 apache 上的应用获取到的IP都是Nginx所 ...

  7. Session技术详解

    1.session简介 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务器程 ...

  8. node http.get请求

    var http = require('http'); var querystring = require('querystring') var url = 'http://www.baidu.com ...

  9. 执行hadoop fs -ls时出现错误RuntimeException: core-site.xml not found

    由于暴力关机,Hadoop fs -ls 出现了下图问题: 问题出现的原因是下面红框框里面的东西,我当时以为从另一个节点下载一个conf.cloudera.yarn文件就能解决问题,发现不行啊,于是删 ...

  10. hdoj1847(博弈论)

    代码: #include<stdio.h>int main(){ int N; while(scanf("%d",&N)!=EOF) printf(N%3==0 ...