Barricade

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

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
最短路+网络流。
先一遍bfs找到最短路,再一次bfs找到最短路上的点,通过dis[i]+1 = dis[u]来找,然后跑一遍Dinic。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int maxn = ;
const int inf = 0x3f3f3f3f;
int n,m;
int g[maxn][maxn];
int vis[maxn];
int dis[maxn];
struct edge
{
int to;
int cap;
int rev;
};
vector<edge> gg[maxn];
int level[maxn];
int it[maxn];
void add(int from,int to,int cap)
{
edge cur;
cur.to = to;
cur.cap = cap;
cur.rev = gg[to].size();
gg[from].push_back(cur);
cur.to = from;
cur.cap = ;
cur.rev = gg[from].size()-;
gg[to].push_back(cur);
} void bfs(int s)
{
memset(level,-,sizeof(level));
queue<int> q;
level[s] = ;
q.push(s);
while(!q.empty())
{
int v = q.front(); q.pop();
for(int i=;i<gg[v].size();i++)
{
edge &e = gg[v][i];
if(e.cap>&&level[e.to]<)
{
level[e.to] = level[v]+;
q.push(e.to);
}
}
}
}
int dfs(int v,int t,int f)
{
if(v==t) return f;
for(int &i=it[v];i<gg[v].size();i++)
{
edge &e = gg[v][i];
if(e.cap>&&level[v]<level[e.to])
{
int d = dfs(e.to,t,min(f,e.cap));
if(d>)
{
e.cap -= d;
gg[e.to][e.rev].cap += d;
return d;
}
}
}
return ;
}
int max_flow(int s,int t)
{
int flow = ;
for(;;)
{
bfs(s);
if(level[t]<) return flow;
memset(it,,sizeof(it));
int f;
while((f=dfs(s,t,inf))>) flow += f;
}
}
bool bfs1()
{
queue<int> q;
memset(vis,,sizeof(vis));
memset(dis,inf,sizeof(dis));
vis[] = ;
dis[] = ;
q.push();
while(!q.empty())
{
int cur = q.front();q.pop();
if(cur==n) return true;
for(int i=;i<=n;i++)
{
if(cur==i) continue;
if(!vis[i]&&g[cur][i]!=-)
{
vis[i] = ;
dis[i] = dis[cur]+;
q.push(i);
}
}
}
return false;
}
void bfs2()
{
queue<int> q;
memset(vis,,sizeof(vis));
vis[n] = ;
q.push(n);
while(!q.empty())
{
int cur = q.front();q.pop();
for(int i=;i<=n;i++)
{
if(cur==i) continue;
if(g[cur][i]==-) continue;
if(dis[i]+==dis[cur])
{
add(i,cur,g[i][cur]);
if(!vis[i])
{
vis[i] = ;
q.push(i);
}
}
}
}
}
int main()
{
int T;cin>>T;
while(T--)
{
scanf("%d %d",&n,&m);
int u,v,w;
memset(g,-,sizeof(g));
for(int i=;i<maxn;i++) gg[i].clear();
for(int i=;i<=m;i++)
{
scanf("%d %d %d",&u,&v,&w);
g[u][v] = w;
g[v][u] = w;
}
int ans = ;
bfs1();
bfs2();
ans = max_flow(,n);
printf("%d\n",ans);
}
return ;
}
 

HDU 5889 (最短路+网络流)的更多相关文章

  1. HDU 5889 Barricade(最短路+最小割水题)

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

  2. ACM: HDU 2544 最短路-Dijkstra算法

    HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  3. UESTC 30 &&HDU 2544最短路【Floyd求解裸题】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. hdu 5521 最短路

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  5. HDU 3605 Escape (网络流,最大流,位运算压缩)

    HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...

  6. HDU 4289 Control (网络流,最大流)

    HDU 4289 Control (网络流,最大流) Description You, the head of Department of Security, recently received a ...

  7. HDU 4292 Food (网络流,最大流)

    HDU 4292 Food (网络流,最大流) Description You, a part-time dining service worker in your college's dining ...

  8. HDU - 2544最短路 (dijkstra算法)

    HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...

  9. 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 ...

随机推荐

  1. js 技巧

    用于浮窗跳转至父窗口 parent.document.location.href='/xxx/xxx.htm'; 取父窗口的元素 window.parent.$('#xxx'); 正常跳转 windo ...

  2. Hadoop作业优化

    mapper数量 reduce数量 combiner 中间值压缩 自定义序列 调整shuffle,减少溢出写 关闭推测执行 任务JVM重用 慢启动reduce

  3. CSS 各类 块级元素 行级元素 水平 垂直 居中问题

    元素的居中问题是每个初学者碰到的第一个大问题,在此我总结了下各种块级 行级 水平 垂直 的居中方法,并尽量给出代码实例. 首先请先明白块级元素和行级元素的区别 行级元素 一块级元素 1 水平居中: ( ...

  4. 嵌套json

    今天一个客户问怎样把表中相同键值对应的文本按照一定顺序拼接起来.如果使用SQL实现将非常麻烦,并且效率低下.GP4.1以后提供了一个函数array_agg可以方便快捷,高效的实现该功能 比如原始查询是 ...

  5. MySQL python组件安装

    可使用pip进行安装 pip install MySQL-python 如出现以下错误 _mysql.c::: 错误:my_config.h:没有那个文件或目录 _mysql.c::: 错误:mysq ...

  6. Windows下为Python编译C扩展模块

    工具:CodeBlocks 13.12 步骤 1 打开CodeBlocks新建工程:Shared library   --   c    --  sample    [默认GUN GCC Compli ...

  7. 在选定的数据源上未找到名为“TitleSub”的字段或属

    在.NET开发过程中时常会遇到“在选定的数据源上未找到名为“TitleSub”的字段或属性”的错误”,导致这类错误的原因有很多,在我的项目中,详细情况是这样:1.有两个控件:DropDownList类 ...

  8. linux文件特殊属性介绍(s,s,t)

    文件的权限有rwx这3个读.写.执行的权限.但是,怎么 /tmp权限有些奇怪?还有, /usr/bin/passwd也有些奇怪,怎么回事呢? [root@linux ~]# ls -ld /tmp ; ...

  9. java代码中 路径符号的写法

    String path="D:\\新建文件夹\\2.png"; File file=new File(path); System.out.println(file.exists() ...

  10. Linux入门学习教程:虚拟机体验之KVM篇

    本文中可以学习到的命令: 1. aptitude 是apt-get 不会产生垃圾的版本 2.       dpkg -L virtualbox 显示属于该包的文件 lsmod | grep kvmfi ...