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. LOJ 534 花团(线段树+dfs栈)

    题意 https://loj.ac/problem/534 思路 又是复杂度错误的一题,\(O(n^2\log n)\) 能过 \(15000\) . 虽然看起来强制在线,其实是一道假的在线题.首先按 ...

  2. Python实现机器学习算法:感知机

    ''' 数据集:Mnist 训练集数量:60000 测试集数量:10000 ------------------------------ 运行结果: 正确率:81.72%(二分类) ''' impor ...

  3. Codeforces 884C.Bertown Subway ----判环,思路

    The construction of subway in Bertown is almost finished! The President of Berland will visit this c ...

  4. BZOJ 3673: 可持久化并查集(可持久化并查集+启发式合并)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3673 题意: 思路: 可持久化数组可以用可持久化线段树来实现,并查集的查询操作和原来的一般并查集操作 ...

  5. Win10远程桌面可能是由于CredSSP加密Oracle修正

    win10更新1083之后,远程桌面就会连接失败,显示如下: 根据微软官方的说法是更改了安全策略: https://support.microsoft.com/zh-cn/help/4093492/c ...

  6. js实现可视化区域内拖拽

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  7. 切片对象的demo

    a = slice(, ) s = 'HelloWorld' print(a.indices(len(s))) for i in range(*a.indices(len(s))): print(s[ ...

  8. MySQL数据分组Group By 和 Having

    现有以下的学生信息表: 若果现在想计算每个班的平均年龄,使用where的操作如下: SELECT Cno AS 班级, AVG(Sage) AS 平均年龄 FROM stu ; 这样的话,有多少个班就 ...

  9. Codeforces 1005 F - Berland and the Shortest Paths

    F - Berland and the Shortest Paths 思路: bfs+dfs 首先,bfs找出1到其他点的最短路径大小dis[i] 然后对于2...n中的每个节点u,找到它所能改变的所 ...

  10. 优雅地记录Python程序日志2:模块组件化日志记录器

    本文摘自:https://zhuanlan.zhihu.com/p/32043593 本篇将会涉及: logging的各个模块化组件 构建一个组件化的日志器 logging的模块组件化 在上一篇文章中 ...