HDU 5294 Tricks Device 网络流 最短路
Tricks Device
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5294
Description
Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the entrance of the tomb while Dumb Zhang’s at the end of it. The tomb is made up of many chambers, the total number is N. And there are M channels connecting the chambers. Innocent Wu wants to catch up Dumb Zhang to find out the answers of some questions, however, it’s Dumb Zhang’s intention to keep Innocent Wu in the dark, to do which he has to stop Innocent Wu from getting him. Only via the original shortest ways from the entrance to the end of the tomb costs the minimum time, and that’s the only chance Innocent Wu can catch Dumb Zhang.
Unfortunately, Dumb Zhang masters the art of becoming invisible(奇门遁甲) and tricks devices of this tomb, he can cut off the connections between chambers by using them. Dumb Zhang wanders how many channels at least he has to cut to stop Innocent Wu. And Innocent Wu wants to know after how many channels at most Dumb Zhang cut off Innocent Wu still has the chance to catch Dumb Zhang.
Input
There are multiple test cases. Please process till EOF.
For each case,the first line must includes two integers, N(<=2000), M(<=60000). N is the total number of the chambers, M is the total number of the channels.
In the following M lines, every line must includes three numbers, and use ai、bi、li as channel i connecting chamber ai and bi(1<=ai,bi<=n), it costs li(0<li<=100) minute to pass channel i.
The entrance of the tomb is at the chamber one, the end of tomb is at the chamber N.
Output
Output two numbers to stand for the answers of Dumb Zhang and Innocent Wu’s questions.
Sample Input
8 9
1 2 2
2 3 2
2 4 1
3 5 3
4 5 4
5 8 1
1 6 2
6 7 5
7 8 1
Sample Output
2 6
Hint
题意
给一个无向图,然后问你最少删除多少个边使得最短路改变。
最多删除多少条边使得最短路仍然不变。
题解:
第一个问题,我们把所有最短路的边扔去跑最小割就好了。
第二个问题,跑一个dij然后除了最短的那条最短路以外,其他的边都删除就好了。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000 + 50;
struct edge{
int v , nxt , w;
}e[60000 * 3];
struct node{
int x , y , z;
friend bool operator < (const node & a ,const node & b){
return a.y > b.y || ( a.y == b.y && a.z > b.z );
}
node(int x = 0 ,int y = 0 , int z = 0) : x(x) , y(y) , z(z) {}
};
int n , m , head[maxn] , tot , flag[maxn];
pair < int , int > dp1[maxn] , dp2[maxn];
int E1[60000*2+5],E2[60000*2+5],E3[60000*2+5];
namespace NetFlow
{
const int MAXN=100000,MAXM=1000000,inf=1e9;
struct Edge
{
int v,c,f,nx;
Edge() {}
Edge(int v,int c,int f,int nx):v(v),c(c),f(f),nx(nx) {}
} E[MAXM];
int G[MAXN],cur[MAXN],pre[MAXN],dis[MAXN],gap[MAXN],N,sz;
void init(int _n)
{
N=_n,sz=0; memset(G,-1,sizeof(G[0])*N);
}
void link(int u,int v,int c)
{
E[sz]=Edge(v,c,0,G[u]); G[u]=sz++;
E[sz]=Edge(u,0,0,G[v]); G[v]=sz++;
}
bool bfs(int S,int T)
{
static int Q[MAXN]; memset(dis,-1,sizeof(dis[0])*N);
dis[S]=0; Q[0]=S;
for (int h=0,t=1,u,v,it;h<t;++h)
{
for (u=Q[h],it=G[u];~it;it=E[it].nx)
{
if (dis[v=E[it].v]==-1&&E[it].c>E[it].f)
{
dis[v]=dis[u]+1; Q[t++]=v;
}
}
}
return dis[T]!=-1;
}
int dfs(int u,int T,int low)
{
if (u==T) return low;
int ret=0,tmp,v;
for (int &it=cur[u];~it&&ret<low;it=E[it].nx)
{
if (dis[v=E[it].v]==dis[u]+1&&E[it].c>E[it].f)
{
if (tmp=dfs(v,T,min(low-ret,E[it].c-E[it].f)))
{
ret+=tmp; E[it].f+=tmp; E[it^1].f-=tmp;
}
}
}
if (!ret) dis[u]=-1; return ret;
}
int dinic(int S,int T)
{
int maxflow=0,tmp;
while (bfs(S,T))
{
memcpy(cur,G,sizeof(G[0])*N);
while (tmp=dfs(S,T,inf)) maxflow+=tmp;
}
return maxflow;
}
}
using namespace NetFlow;
void My_init(){
for(int i = 1 ; i <= n ; ++ i) head[i] = -1 , dp1[i].first = 1<<29 , dp2[i].first = 1<<29 , flag[i] = 0;
tot = 0;
}
void Edge_link(int u , int v , int w){
e[tot].v=v,e[tot].nxt=head[u],e[tot].w=w,head[u]=tot++;
}
void Dijkstra( int start , pair < int , int > * p ){
priority_queue<node>Q;
Q.push(node( start , 0 , 0 ));
p[start]=make_pair(0,0);
while(!Q.empty()){
node S = Q.top() ; Q.pop();
int x = S.x;
pair < int , int > Ls = make_pair( S.y , S.z );
if( Ls != p[x] ) continue;
for(int i = head[x] ; ~i ; i = e[i].nxt){
int v = e[i].v;
int w = e[i].w;
pair < int , int > newLs = make_pair( Ls.first + w , Ls.second + 1 );
if( newLs.first < p[v].first || (newLs.first == p[v].first && newLs.second < p[v].second)){
p[v] = newLs;
Q.push( node( v , newLs.first , newLs.second ) );
}
}
}
}
int main(int argc,char *argv[]){
while( ~ scanf("%d%d" , &n , &m ) ){
My_init();
for(int i = 1 ; i <= m ; ++ i){
int u , v , w;
scanf("%d%d%d",&u,&v,&w);
E1[i]=u,E2[i]=v,E3[i]=w;
Edge_link( u , v , w );
Edge_link( v , u , w );
}
Dijkstra( 1 , dp1 );
Dijkstra( n , dp2 );
int mincost = dp1[n].first;
init( n + 4 );
for(int i = 1 ; i <= m ; ++ i)
{
if(dp1[E1[i]].first + dp2[E2[i]].first + E3[i] == mincost)
link(E1[i],E2[i],1);
if(dp2[E1[i]].first + dp1[E2[i]].first + E3[i] == mincost)
link(E2[i],E1[i],1);
}
printf("%d %d\n" , dinic( 1 , n ) , m - dp1[n].second );
}
return 0;
}
HDU 5294 Tricks Device 网络流 最短路的更多相关文章
- HDU 5294 Tricks Device (最短路,最大流)
题意:给一个无向图(连通的),张在第n个点,吴在第1个点,‘吴’只能通过最短路才能到达‘张’,两个问题:(1)张最少毁掉多少条边后,吴不可到达张(2)吴在张毁掉最多多少条边后仍能到达张. 思路:注意是 ...
- HDU 5294 Tricks Device (最大流+最短路)
题目链接:HDU 5294 Tricks Device 题意:n个点,m条边.而且一个人从1走到n仅仅会走1到n的最短路径.问至少破坏几条边使原图的最短路不存在.最多破坏几条边使原图的最短路劲仍存在 ...
- hdu 5294 Tricks Device 最短路建图+最小割
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294 Tricks Device Time Limit: 2000/1000 MS (Java/Other ...
- HDU 5294 Tricks Device(多校2015 最大流+最短路啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294 Problem Description Innocent Wu follows Dumb Zha ...
- HDU 5294 Tricks Device 最短路+最大流
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5294 题意: 给你个无向图: 1.求最少删除几条边就能破坏节点1到节点n的最短路径, 2.最多能删除 ...
- hdu 5294 Tricks Device(2015多校第一场第7题)最大流+最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294 题意:给你n个墓室,m条路径,一个人在1号墓室(起点),另一个人在n号墓室(终点),起点的那 ...
- SPFA+Dinic HDOJ 5294 Tricks Device
题目传送门 /* 题意:一无向图,问至少要割掉几条边破坏最短路,问最多能割掉几条边还能保持最短路 SPFA+Dinic:SPFA求最短路时,用cnt[i]记录到i最少要几条边,第二个答案是m - cn ...
- HDOJ 5294 Tricks Device 最短路(记录路径)+最小割
最短路记录路径,同一时候求出最短的路径上最少要有多少条边, 然后用在最短路上的边又一次构图后求最小割. Tricks Device Time Limit: 2000/1000 MS (Java/Oth ...
- HDU5294——Tricks Device(最短路 + 最大流)
第一次做最大流的题目- 这题就是堆模板 #include <iostream> #include <algorithm> #include <cmath> #inc ...
随机推荐
- centos7安装libvirt支持xen
另外还有一个非常棒的用法 假如我要执行iostat这个命令来查看CPU与存储设备状态,可是执行却发现没有这个命令 于是执行yum install iostat,结果说找不到该软件,使用下面的办法可以解 ...
- hadoop安装 伪分布
伪分布hadoop 安装总结 准备,在配置中hadoop用的9000端口,如果有其它软件用着这个端口,建议更换后再进行下面配置,以避免出现错误.比如php-fpm经常使用9000端口. 一.下载jdk ...
- 【bzoj3224】普通平衡树
看有没有人能发现咯. #include<bits/stdc++.h> #define N 300005 #define rat 4 #define pushup(o) if(o->l ...
- 设计模式之笔记--原型模式(Prototype)
原型模式(Prototype) 定义 原型模式(Prototype),用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 类图 描述 提供一个克隆自身的接口--Clone方法. 应用场景 ...
- webIcon
webIcon是我在拿别人的模板参考的时候我发现的一个东西,觉得挺不错的一个东西,但是后来发现用webIcon其实我也不知道是好还是不好,因为要用到字体,字体文件其实挺大的,所以当你要的图标不多的时候 ...
- System.Web.HttpContext.Current.Request用法
public static void SetRegisterSource() { if (System.Web.HttpContext.Current.Request["website&qu ...
- qtp录制时间控件不允许用户手动输入的解决办法
qtp录制时间控件不允许用户手动输入的解决办法 [前面的话] 一边学习qtp,一边用自己的项目试着写代码,而遇到一个问题就会让自己卡壳很久,这次也是这样的,在写好了登录代码以后,自己就试着写第一个预订 ...
- AC日记——[SDOI2009]HH的项链 洛谷 P1972
[SDOI2009]HH的项链 思路: 莫队: 代码: #include <bits/stdc++.h> #define maxn 100005 #define maxm 400005 # ...
- Codeforces Round #496 (Div. 3) E2 - Median on Segments (General Case Edition)
E2 - Median on Segments (General Case Edition) 题目大意:给你一个数组,求以m为中位数的区间个数. 思路:很巧秒的转换,我们把<= m 数记为1, ...
- win 10 下面安装 mysql-8.0.12-winx64 的过程
win 10 下面安装 mysql-8.0.12-winx64 的过程 1.官网下载 mysql 2.解压到你要安装的目录 3.在mysql目录D:\Programming\mysql-8.0.12- ...