Barricade

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

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
 

题目链接:HDU 5889

去年的青岛网络赛一道题,当时只会最短路算法就不会做这水题…………,然而其他几道真正的水题当时也不会做……真是太差劲了……

跑一遍最短路后把最短路上的边加入网络流计算最小割即可,题目说边权相同直接BFS出最短距离就好了。

把dx打成d狂TLE(我可能做了假题目),然后这题有个细节要注意一下,虽然给的是无向图,但是最短路网络中要是有向的,如果当成双向边加入网络流就会WA。

看下这个例子就知道了,实际上1-2-3-4这条路径并不是到最短路径,但由于双向边的关系会使得这条路径连通,导致多出来的1流量流到4

对应数据:

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

若添加双向边到网络流中则答案变成2,正确的为1

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1010;
const int M = 10010;
struct Edge
{
int to, nxt, dx, data;
Edge() {}
Edge(int _to, int _nxt, int _dx, int _data): to(_to), nxt(_nxt), dx(_dx), data(_data) {}
};
struct edge
{
int to, nxt, cap;
edge() {}
edge(int _to, int _nxt, int _cap): to(_to), nxt(_nxt), cap(_cap) {}
};
Edge E[M << 1];
edge e[M << 1];
int H[N], h[N], tot, Tot;
int dx[N], d[N]; void init()
{
CLR(H, -1);
CLR(h, -1);
tot = 0;
Tot = 0;
CLR(dx, INF);
}
inline void addE(int s, int t, int D, int c)
{
E[Tot] = Edge(t, H[s], D, c);
H[s] = Tot++;
}
inline void adde(int s, int t, int c)
{
e[tot] = edge(t, h[s], c);
h[s] = tot++;
e[tot] = edge(s, h[t], 0);
h[t] = tot++;
}
void BFS(int s)
{
queue<int>Q;
Q.push(s);
dx[s] = 0;
while (!Q.empty())
{
int u = Q.front();
Q.pop();
for (int i = H[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if (dx[v] == INF)
{
dx[v] = dx[u] + 1;
Q.push(v);
}
}
}
}
int bfs(int s, int t)
{
CLR(d, -1);
d[s] = 0;
queue<int>Q;
Q.push(s);
while (!Q.empty())
{
int u = Q.front();
Q.pop();
for (int i = h[u]; ~i; i = e[i].nxt)
{
int v = e[i].to;
if (d[v] == -1 && e[i].cap > 0)
{
d[v] = d[u] + 1;
if (v == t)
return 1;
Q.push(v);
}
}
}
return ~d[t];
}
int dfs(int s, int t, int f)
{
if (s == t || !f)
return f;
int ret = 0;
for (int i = h[s]; ~i; i = e[i].nxt)
{
int v = e[i].to;
if (d[v] == d[s] + 1 && e[i].cap > 0)
{
int df = dfs(v, t, min(f, e[i].cap));
if (df > 0)
{
e[i].cap -= df;
e[i ^ 1].cap += df;
ret += df;
f -= df;
if (!f)
break;
}
}
}
if (!ret)
d[s] = -1;
return ret;
}
int dinic(int s, int t)
{
int ret = 0;
while (bfs(s, t))
ret += dfs(s, t, INF);
return ret;
}
int main(void)
{
int tcase, n, m, a, b, c, i;
scanf("%d", &tcase);
while (tcase--)
{
init();
scanf("%d%d", &n, &m);
for (i = 0; i < m; ++i)
{
scanf("%d%d%d", &a, &b, &c);
addE(a, b, 1, c);
addE(b, a, 1, c);
}
BFS(1);
for (int u = 1; u <= n; ++u)
{
for (int j = H[u]; ~j; j = E[j].nxt)
{
int v = E[j].to;
if (dx[v] - dx[u] == 1)
adde(u, v, E[j].data);
}
}
printf("%d\n", dinic(1, n));
}
return 0;
}

HDU 5889 Barricade(最短路+最小割水题)的更多相关文章

  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. Barricade HDU - 5889(最短路+最小割)

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

  3. hdu 6852Path6(最短路+最小割)

    传送门 •题意 有n个城市,标号1-n 现花费最小的代价堵路 使得从1号城市到n号城市的路径边长 (注意只是变长不是最长) 堵一条路的代价是这条路的权值 •思路 在堵路以前,从1到n的最小路径当然是最 ...

  4. HDU - 6582 Path (最短路+最小割)

    题意:给定一个n个点m条边的有向图,每条边有个长度,可以花费等同于其长度的代价将其破坏掉,求最小的花费使得从1到n的最短路变长. 解法:先用dijkstra求出以1为源点的最短路,并建立最短路图(只保 ...

  5. HDU 3452 Bonsai(网络流之最小割)

    题目地址:HDU 3452 最小割水题. 源点为根节点.再另设一汇点,汇点与叶子连边. 对叶子结点的推断是看度数是否为1. 代码例如以下: #include <iostream> #inc ...

  6. 【bzoj1266】[AHOI2006]上学路线route 最短路+最小割

    题目描述 可可和卡卡家住合肥市的东郊,每天上学他们都要转车多次才能到达市区西端的学校.直到有一天他们两人参加了学校的信息学奥林匹克竞赛小组才发现每天上学的乘车路线不一定是最优的. 可可:“很可能我们在 ...

  7. HDU 5832 A water problem(某水题)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  8. HDU 5889 Barricade(最短路+最小割)

    http://acm.hdu.edu.cn/showproblem.php?pid=5889 题意: 给出一个图,帝国将军位于1处,敌军位于n处,敌军会选择最短路到达1点.现在帝国将军要在路径上放置障 ...

  9. [2019杭电多校第一场][hdu6582]Path(最短路&&最小割)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6582 题意:删掉边使得1到n的最短路改变,删掉边的代价为该边的边权.求最小代价. 比赛时一片浆糊,赛后 ...

随机推荐

  1. SqlServer触发器的理解

    SqlServer触发器是与表事件相关的特殊存储过程,它的执行不是由程序调用,也不是手工启动,而是由事件来触发.比如当对一个表进行操作( insert,delete, update)时就会激活它执行. ...

  2. GB MB KB B 关系

    1KB=1024Bytes=2的10次方Bytes 1MB=1024KB=2的20次方Bytes 1GB=1024MB=2的30次方Bytes 1TB=1024GB=2的40次方Bytes

  3. Websocket教程SpringBoot+Maven整合(详情)

    1.大话websocket及课程介绍 简介: websocket介绍.使用场景分享.学习课程需要什么基础 笔记: websocket介绍: WebSocket协议是基于TCP的一种新的网络协议.它实现 ...

  4. 黑马基础阶段测试题:创建Phone(手机)类,Phone类中包含以下内容:

    package com.swift; public class Phone { private String pinpai; private int dianliang; public String ...

  5. Linux磁盘I/O性能监控——iostat

    iostat命令可以查看CPU利用率和磁盘性能相关数据,有时候我们会觉得系统响应慢,传数据很慢,这个慢可能是多方面原因导致的,如CPU利用率高.网络差.系统平均负载高甚至是磁盘已经损坏了.对此,系统性 ...

  6. Sum All Primes-freecodecamp算法题目

    Sum All Primes 1.要求 求小于等于给定数值的质数之和. 只有 1 和它本身两个约数的数叫质数.例如,2 是质数,因为它只能被 1 和 2 整除.1 不是质数,因为它只能被自身整除. 2 ...

  7. PAT 乙级 1015

    题目 题目地址:PAT 乙级 1015 题解 常规题,难点在于理清楚排序规则,通过比较简洁的方式进行编码: 在这里我选择使用vector进行存储,并使用sort方法排序,因为本题不是简单按照大小排序, ...

  8. JS - OOP-继承的最佳实现方式

    如上图,使用第三种方式实现继承最好,也就是加了下划线的. 但是Object.create方法是ES6才支持的,所以,右边就写了一个实现其同样功能的函数.

  9. Python_三级目录

    程序要求: 1. 使用字典存储 1. 可以一层一层的进入到所有层2. 可以在每层返回上一层3. 可以在任意层退出 三级目录写了两个版本,第一个版本是刚看完字典写出来的,代码很多冗余,很多重复. men ...

  10. vue 中有时候是数据没有同步的问题

    1,在项目中,在做表格的数据渲染的时候,表格中有input标签的数据来进行双向绑定, this.$set(this.tableTitle.money, index, money[index]+isMo ...