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. 用Micro:bit控制遥控车

    很多遥控车是用Arduino来控制,同样也可以用Micro:bit来控制.这篇文章我们就来做个测试. 这次需要用到扩展板,管脚比较多,请参考下图 一.材料: •micro:bit 二片 •micro: ...

  2. python爬虫beautifulsoup4系列2

    前言 本篇详细介绍beautifulsoup4的功能,从最基础的开始讲起,让小伙伴们都能入门 一.读取HTML页面 1.先写一个简单的html页面,把以下内容copy出来,保存为html格式文件 &l ...

  3. Python3列表中获取相同元素出现位置的下标

    前言 list: Python3的列表类型, 和其他语言中的数组类似 定义格式: l = ["a", "b", "c", "a&q ...

  4. Arthas Alibaba 开源 Java 诊断工具

    Arthas 用户文档 English Docs Arthas(阿尔萨斯) 能为你做什么? Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱. 当你遇到以下类似问题而束手无策时,A ...

  5. Datawhale MySQL 训练营 Task4 表联结

    学习内容 MySQL别名 列别名,将查询或者筛选出来列用AS 命名,如果有空格则需要引号 '' SELECT xxx AS xxxx FROM WHERE GROUP BY HAVING 表别名, 把 ...

  6. 用可道云kodexplorer在dedecms系统网站上秒建私人网盘

    国内草根站长用的最多的一款建站源程序就是dedecms,通常是通过FTP或者服务器面板自带的文件管理器来上传下载的.FTP可视性.体验都相对差一点,且需要事先安装FTP软件,更换环境后的站点管理上有很 ...

  7. python-python爬取豆果网(菜谱信息)

    #-*- coding = utf-8 -*- #获取豆果网图片 import io from bs4 import BeautifulSoup import requests #爬取菜谱的地址 ur ...

  8. caffe 预训练 或者Fine-Tuning 操作

    1.使用预训练模型,需要修改训练的prototxt,将layer name改为与要使用模型的layer name相同即可. Borrowing Weights from a Pretrained Ne ...

  9. Homebrew -- 安装与使用

    使用 React Native,必须安装的依赖有:Node.Watchman 和 React Native 命令行工具以及 Xcode. 推荐使用Homebrew来安装 Node 和 Watchman ...

  10. BugPhobia贡献篇章:团队贡献分值与转会确定

    0x01 :无言 0x02 :团队贡献分说明 (1202)冯志睿 54 (1156)李入云 43 (1188)李云涛 56 (1184)马腾跃 26 (1197)钱林琛 60 (1100)王鹿鸣 63 ...