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. Altium 中异形焊盘异形封装的创建图文教程

    Altium 中异形焊盘异形封装的创建图文教程 一般不规则的焊盘被称为异型焊盘,典型的有金手指.大型的器件焊盘或者板子上需要添加特殊形状的铜箔(可以制作一个特殊封装代替). 如图27所示,此处我们以一 ...

  2. CSS盒模型 flex

    用于网页布局,PC的话,兼容性不够,慎用,手机端的话,神器 整理部分通用的,可以直接复制的,省得下次再写一遍 注意,设为 Flex 布局以后,子元素的float.clear和vertical-alig ...

  3. [硬件配置]记录Ubuntu 14.04 下安装无线网卡驱动解决无法连接WiFi的过程

    新电脑安装了Ubuntu 14.04,但是网络连接中只有以太网而没有WiFi的选项. 打开System Setting系统设置-Software&Updates软件&更新-Additi ...

  4. Go简单聊天

    用Go简单实现网络通信 其余功能可以在这个模型上继续加,比如增加通信人数,实现聊天 server 端 package main import ( "fmt" "log&q ...

  5. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  6. 小强版之无码理解C语言指针

     1. 先从普通变量开始   2. 撸完变量撸指针   3. 故事情节进一步发展,此处少儿不宜   4. 奶茶妹妹捉奸,小强死定了   5. 源码欣赏  #include <stdio.h> ...

  7. CentOS查看一共安装了多少软件包,是那些软件包

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/48292853 一.如何得知共安装了多少个软件包 [root@localhost ~ ...

  8. Java 内存模型_1

    title: Java 内存模型_1 date: 2017-01-15 17:11:02 tags: [JMM] categories: [Programming,Java] --- 概述 本文记录 ...

  9. 局域网arpspoof欺骗获取cookie/图片/密码

    开启路由转发功能 查看IP转发功能是否打开 默认是不开起,0,我这里是修改后的,显示1. 修改转发功能,1为允许. 修改成功后再进行Arpspoof欺骗 如果开始劫持后,自己电脑无法联网了 ??? 检 ...

  10. Centos7 Jenkins

    代码上线 持续集成 随时随地将代码合并,这种方法叫做持续集成. 持续集成(CONTINUOUS INTEGRATION,简称CI)持续集成指的是,频繁地(一天多次)将代码集成到主干.它的好处主要有两个 ...