Barricade

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

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
 
    给出一个无向图,每条边的长度都是1,第i条边建立障碍费用为wi,在保证从1到N号点的所有
的最短路径上都有障碍的情况下使得花费最小,输出这个费用。
  将所有非最短路上的边都去除之后,问题转化为求当前图的最小割(因为要所有S-T路径上都至
少出现一条边是是障碍边,也就是说将障碍边去除之后S-T不在联通),跑一下最大流就好了。
 
  

 #include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define mp make_pair
struct Edge
{
int v,cap,flow,next;
}e[];
vector<int> g[];
int first[],d[],cur[],tot,N;
bool vis[];
void add(int u,int v,int cap){
e[tot]=Edge{v,cap,,first[u]};
first[u]=tot++;
}
int dij(){
memset(d,inf,sizeof(d));
memset(vis,,sizeof(vis));
d[]=;
priority_queue<pii,vector<pii>,greater<pii> >q;
q.push(mp(,));
while(!q.empty()){
int u=q.top().second;q.pop();
if(vis[u]) continue;
vis[u]=;
for(int i=;i<g[u].size();++i){
if(d[g[u][i]]>d[u]+){
d[g[u][i]]=d[u]+;
q.push(mp(d[g[u][i]],g[u][i]));
}
}
}
return d[N];
}
bool bfs(){
memset(d,,sizeof(d));
memset(vis,,sizeof(vis));
queue<int>q;
q.push();
d[]=;
vis[]=;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=first[u];~i;i=e[i].next){
if(!vis[e[i].v] && e[i].cap-e[i].flow>){
vis[e[i].v]=;
d[e[i].v]=d[u]+;
q.push(e[i].v);
}
}
}
return vis[N];
}
int dfs(int u,int a){
if(u==N || a==) return a;
int f,ans=;
for(int &i=cur[u];~i;i=e[i].next){
if(d[e[i].v]==d[u]+ && (f=dfs(e[i].v,min(a,e[i].cap-e[i].flow)))>){
e[i].flow+=f;
e[i^].flow-=f;
a-=f;
ans+=f;
if(!a) break;
}
}
return ans;
}
void solve(){
int ans=;
while(bfs()){
for(int i=;i<=N;++i) cur[i]=first[i];
ans+=dfs(,inf);
}
printf("%d\n",ans);
}
int main(){
int M,t,i,j,k;
int u[],v[],w[];
cin>>t;
while(t--){
scanf("%d%d",&N,&M);
memset(first,-,sizeof(first));
tot=;
for(i=;i<=N;++i) g[i].clear();
for(i=;i<=M;++i){
scanf("%d%d%d",u+i,v+i,w+i);
g[u[i]].push_back(v[i]);
g[v[i]].push_back(u[i]);
}
dij();
for(i=;i<=M;++i){
if(d[u[i]]+==d[v[i]]){
add(u[i],v[i],w[i]),add(v[i],u[i],); }
if(d[v[i]]+==d[u[i]]){
add(v[i],u[i],w[i]),add(u[i],v[i],);
}
}
solve();
}
return ;
}

hdu-5889-最短路+网络流/最小割的更多相关文章

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

    Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  2. HDU 5889 (最短路+网络流)

    Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  3. 【题解】 bzoj3894: 文理分科 (网络流/最小割)

    bzoj3894,懒得复制题面,戳我戳我 Solution: 首先这是一个网络流,应该还比较好想,主要就是考虑建图了. 我们来分析下题面,因为一个人要么选文科要么选理科,相当于两条流里面割掉一条(怎么 ...

  4. 【bzoj3774】最优选择 网络流最小割

    题目描述 小N手上有一个N*M的方格图,控制某一个点要付出Aij的代价,然后某个点如果被控制了,或者他周围的所有点(上下左右)都被控制了,那么他就算是被选择了的.一个点如果被选择了,那么可以得到Bij ...

  5. 【bzoj1143】[CTSC2008]祭祀river Floyd+网络流最小割

    题目描述 在遥远的东方,有一个神秘的民族,自称Y族.他们世代居住在水面上,奉龙王为神.每逢重大庆典, Y族都会在水面上举办盛大的祭祀活动.我们可以把Y族居住地水系看成一个由岔口和河道组成的网络.每条河 ...

  6. 【bzoj1797】[Ahoi2009]Mincut 最小割 网络流最小割+Tarjan

    题目描述 给定一张图,对于每一条边询问:(1)是否存在割断该边的s-t最小割 (2)是否所有s-t最小割都割断该边 输入 第一行有4个正整数,依次为N,M,s和t.第2行到第(M+1)行每行3个正 整 ...

  7. 【bzoj1976】[BeiJing2010组队]能量魔方 Cube 网络流最小割

    题目描述 一个n*n*n的立方体,每个位置为0或1.有些位置已经确定,还有一些需要待填入.问最后可以得到的 相邻且填入的数不同的点对 的数目最大. 输入 第一行包含一个数N,表示魔方的大小. 接下来 ...

  8. 【bzoj4177】Mike的农场 网络流最小割

    题目描述 Mike有一个农场,这个农场n个牲畜围栏,现在他想在每个牲畜围栏中养一只动物,每只动物可以是牛或羊,并且每个牲畜围栏中的饲养条件都不同,其中第i个牲畜围栏中的动物长大后,每只牛可以卖a[i] ...

  9. 【bzoj3438】小M的作物 网络流最小割

    原文地址:http://www.cnblogs.com/GXZlegend/p/6801522.html 题目描述 小M在MC里开辟了两块巨大的耕地A和B(你可以认为容量是无穷),现在,小P有n中作物 ...

随机推荐

  1. Ubuntu 更新系统版本以及查看当前系统版本的命令

    1. Ubuntu 查看当前系统版本: lsb_release -a 2. Ubuntu 更新系统版本的命令: sudo do-release-upgrade

  2. 洛谷P2782 友好城市 DP

    やはり まだあしたということは嘘でしょう.ぜんぶ忘れた( ´・ヮ・`) 所以今天就贴一道水题吧 原题>>https://www.luogu.org/problem/show?pid=278 ...

  3. 【译】第44节---EF6-存储过程映射

    原文:http://www.entityframeworktutorial.net/entityframework6/code-first-insert-update-delete-stored-pr ...

  4. python3 session cookie

    session是保存在服务器中的,而cookies是保存在客户端中的.服务器通过session id来跟踪客户,而对于客户端而言,session id是保存在cookies中的,所以只要把cookie ...

  5. react-redux的mapStateToProps可取到state值但不会注入props

    一.问题描述 想通过react-redux和redux实现react组件之间的通信,reducer.action.store都编写正确,mapDispatchToProps也能正确传值.唯独mapSt ...

  6. 【汇总】基于.NET平台常用的框架整理

    分布式缓存框架: Microsoft Velocity:微软自家分布式缓存服务框架. Memcahed:一套分布式的高速缓存系统,目前被许多网站使用以提升网站的访问速度. Redis:是一个高性能的K ...

  7. 设计模式(七)Adapter Pattern适配器模式

    适用场景:旧系统的改造升级 实际场景:java.io.InputStreamReader(InputStream)等 1.一个被适配的类 package com.littlepage.AdapterP ...

  8. web自动化测试与Appuim自动化测试对比

    web自动化测试-打开浏览器: from selenium import webdriver driver = webdriver.Chrome() #定义chrome驱动 driver.maximi ...

  9. python3使用pymysql模块,连接mysql数据库,实现新增、查询和更新操作

    1.环境数据准备: python3环境.pymysql模块 mysql数据库:本次代码中用到的数据库为本地的testdb数据库,user表(表字段比较简单,只有主键id,手机号mobile,密码pas ...

  10. STL_map.修改删除

    1.修改示例 int TdrSvgAttr::AttrSet_mem(bool _bAttrInStyle, string &_strAttrName, string& _strAtt ...