Barricade

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

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
 
注意审题
题中说的是敌人会走最短的路 所以我们把所有的最短路都拿出来 跑一边最大流即可
怎样把所有的最短路拿出来 跑一边最短路即可。。。因为跑完之后 起点和终点之间的所有的最短路 可以通过判断 d[e.v] == d[e.u] + 1 来进行建网络流的图
注意建网络流图的方式 不然会t
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int head[maxn], head2[maxn], dis[maxn], d[maxn], vis[maxn], cur[maxn];
int cnt, cnt2;
int n, m, s, t; struct node
{
int u, v, w, c, next;
}Node[maxn<<]; void add_(int u, int v, int w, int c)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].w = w;
Node[cnt].c = c;
Node[cnt].next = head[u];
head[u] = cnt++;
} void add(int u, int v, int w, int c)
{
add_(u, v, w, c);
add_(v, u, w, c);
} void spfa()
{
for(int i=; i<=n; i++) dis[i] = INF;
mem(vis, );
queue<int> Q;
Q.push(s);
vis[s] = ;
dis[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
vis[u] = ;
for(int i=head[u]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(dis[e.v] > dis[u] + e.w)
{
dis[e.v] = dis[u] + e.w;
if(!vis[e.v])
{
vis[e.v] = ;
Q.push(e.v);
}
}
}
}
} struct edge
{
int u, v, c, next;
}Edge[maxn<<]; void add_edge(int u, int v, int c)
{
Edge[cnt2].u = u;
Edge[cnt2].v = v;
Edge[cnt2].c = c;
Edge[cnt2].next = head2[u];
head2[u] = cnt2++;
} void add_Edge(int u, int v, int c)
{
add_edge(u, v, c);
add_edge(v, u, );
} bool bfs()
{
queue<int> Q;
mem(d, );
Q.push(s);
d[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i=head2[u]; i!=-; i=Edge[i].next)
{
edge e = Edge[i];
if(!d[e.v] && e.c > )
{
d[e.v] = d[e.u] + ;
Q.push(e.v);
if(e.v == t) return ;
}
}
}
return d[t] != ;
} int dfs(int u, int cap)
{
int ret = , V;
if(u == t || cap == )
return cap;
for(int &i=cur[u]; i!=-; i=Edge[i].next)
{
edge e = Edge[i];
if(d[e.v] == d[e.u] + && e.c > )
{
int V = dfs(e.v, min(cap, e.c));
Edge[i].c -= V;
Edge[i^].c += V;
ret += V;
cap -= V;
if(cap == ) break;
}
}
if(cap > ) d[u] = -;
return ret;
} int dinic(int u)
{
int ans = ;
while(bfs())
{
memcpy(cur, head2, sizeof(head2));
ans += dfs(u, INF);
}
return ans;
} void build()
{
for(int i=; i<=n; i++)
for(int j=head[i]; j!=-; j=Node[j].next)
{
node e = Node[j];
if(dis[e.v] == dis[e.u] + )
add_Edge(e.u, e.v, e.c);
}
} int main()
{
int T;
rd(T);
while(T--)
{
mem(head, -);
mem(head2, -);
cnt = cnt2 = ;
rd(n); rd(m);
rep(i, , m)
{
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
add(u, v, , c);
}
s = , t = n;
spfa();
build();
printf("%d\n", dinic(s));
}
return ;
}
 
 
 

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

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

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

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

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

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

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

  4. hdu 3870(平面图最小割转最短路)

    Catch the Theves Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65768/32768 K (Java/Others) ...

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

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

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

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

  7. hdu 4289 Control(最小割 + 拆点)

    http://acm.hdu.edu.cn/showproblem.php?pid=4289 Control Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  8. 【求出所有最短路+最小割】【多校第一场】【G题】

    题意 A从1要追在N的 B 只能走最短的路 问B最少切断多少条路可以让A不能过来 问B最多切断多少条路A还是能过来 对于1 求出1到N的所有最短路的路径,对其求最小割 对于2 求出长度最小的最短路即可 ...

  9. BZOJ1266 AHOI2006上学路线(最短路+最小割)

    求出最短路后找出可能在最短路上的边,显然割完边后我们需要让图中这样的边无法构成1到n的路径,最小割即可,非常板子. #include<iostream> #include<cstdi ...

  10. HDU 4859 海岸线(最小割+最大独立点权变形)

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题意: 欢迎来到珠海!由于土地资源越来越紧张,使得许多海滨城市都只能依靠填海来扩展市区以求发展.作为Z市的 ...

随机推荐

  1. zabbix4.0LTS安装配置

    一.环境准备 1.最小化安装centos7.5系统 2.安装常用软件包 yum install -y net-tools vim bash-completion 3.关闭selinux和修改主机名 v ...

  2. 简单的redis工具类

    import java.util.Arrays; import java.util.List;import java.util.Set; import org.apache.commons.lang. ...

  3. JavaScript判断对象是否是NULL(转)

    写js经常会遇到非空判断,看了你不就像风一样的文章 自己没有做总结,特地转载.很有帮助 function isEmpty(obj) { // 检验 undefined 和 null if (!obj ...

  4. python基础教程:包的创建及导入

    包是一种通过用“带点号的模块名”来构造 Python 模块命名空间的方法. 例如,模块名 A.B 表示 A 包中名为 B 的子模块.正如模块的使用使得不同模块的作者不必担心彼此的全局变量名称一样,使用 ...

  5. BAT面试必备——Java 集合类

    本文首发于我的个人博客:尾尾部落 1. Iterator接口 Iterator接口,这是一个用于遍历集合中元素的接口,主要包含hashNext(),next(),remove()三种方法.它的一个子接 ...

  6. Datawhale MySQL 训练营 Task2 查询语句

    目录 MySQL 管理 MySQL 用户管理 参考 数据库管理 SQ查询语句 1. 导入示例数据库,教程 MySQL导入示例数据库 2. 查询语句 SELECT 3. 筛选语句 WHERE ,过滤 4 ...

  7. lsblk命令详解

    基础命令学习目录首页 lsblk 默认是树形方式显示: $lsblk NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTsda      8:0    0   2. ...

  8. iOS 源代码混淆(初步混淆)

    我们可以用classdump对原程序进行 dump,像上篇文章( Class-dump 安装和使用记录(导出应用的头文件)),我们可以看到所有.h 文件全暴露了(如下图) 点击HWAccount.h后 ...

  9. Beta发布 _thunder_文案+美工展示

    作业要求:https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/1366 团队介绍:thunder 组成员及各位博客地址: 1.王航:htt ...

  10. Git基础级介绍

    这篇随笔是在学习了廖雪峰老师的git教程之后写的总结,要看详细的基础级git介绍可以去http://www.liaoxuefeng.com/wiki/0013739516305929606dd1836 ...