最短路记录路径,同一时候求出最短的路径上最少要有多少条边,

然后用在最短路上的边又一次构图后求最小割.

Tricks Device

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1584    Accepted Submission(s): 388

Problem 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
 
Author
FZUACM
 
Source
 

/* ***********************************************
Author :CKboss
Created Time :2015年07月24日 星期五 10时07分09秒
File Name :HDOJ5294.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; typedef pair<int,int> pII; const int INF=0x3f3f3f3f;
const int maxn=2200; int n,m; /*************EDGE********************/ struct Edge
{
int to,next,cost,cap,flow;
}edge[maxn*60],edge2[maxn*60]; int Adj[maxn],Size;
int Adj2[maxn],Size2; void Add_Edge(int u,int v,int c)
{
edge[Size].to=v;
edge[Size].next=Adj[u];
edge[Size].cost=c;
Adj[u]=Size++;
} /********spfa************/ int dist[maxn];
bool inQ[maxn]; vector<int> Pre[maxn]; int spfa(Edge* edge,int* Adj)
{
memset(dist,63,sizeof(dist));
memset(inQ,false,sizeof(inQ));
dist[1]=0;
queue<int> q;
inQ[1]=true;q.push(1); while(!q.empty())
{
int u=q.front();q.pop(); for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(dist[v]>dist[u]+edge[i].cost)
{
Pre[v].clear();
Pre[v].push_back(u); dist[v]=dist[u]+edge[i].cost; if(!inQ[v])
{
inQ[v]=true;
q.push(v);
}
}
else if(dist[v]==dist[u]+edge[i].cost)
{
Pre[v].push_back(u);
}
} inQ[u]=false; }
return dist[n];
} /********************rebuild************************/ void Add_Edge2(int u,int v,int w,int rw=0)
{
edge2[Size2].cost=1;
edge2[Size2].to=v; edge2[Size2].cap=w; edge2[Size2].next=Adj2[u];
edge2[Size2].flow=0; Adj2[u]=Size2++; edge2[Size2].cost=1;
edge2[Size2].to=u; edge2[Size2].cap=w; edge2[Size2].next=Adj2[v];
edge2[Size2].flow=0; Adj2[v]=Size2++;
} bool used[maxn];
int edges; void rebuild()
{
memset(used,false,sizeof(used));
queue<int> q;
q.push(n); used[n]=true;
edges=0; while(!q.empty())
{
int v=q.front(); q.pop();
for(int i=0,sz=Pre[v].size();i<sz;i++)
{
int u=Pre[v][i];
/// u--->v
//cout<<u<<" ---> "<<v<<endl;
edges++;
Add_Edge2(u,v,1); if(used[u]==false)
{
used[u]=true; q.push(u);
}
}
}
} /************************max_flow*******************************/ int gap[maxn],dep[maxn],pre[maxn],cur[maxn]; int sap(int start,int end,int N,Edge* edge=edge2)
{
memset(gap,0,sizeof(gap));
memset(dep,0,sizeof(dep));
memcpy(cur,Adj2,sizeof(Adj2)); int u=start;
pre[u]=-1; gap[0]=N;
int ans=0; while(dep[start]<N)
{
if(u==end)
{
int Min=INF;
for(int i=pre[u];~i;i=pre[edge[i^1].to])
{
if(Min>edge[i].cap-edge[i].flow)
Min=edge[i].cap-edge[i].flow;
}
for(int i=pre[u];~i;i=pre[edge[i^1].to])
{
edge[i].flow+=Min;
edge[i^1].flow-=Min;
}
u=start;
ans+=Min;
continue;
} bool flag=false;
int v;
for(int i=cur[u];~i;i=edge[i].next)
{
v=edge[i].to;
if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u])
{
flag=true;
cur[u]=pre[v]=i;
break;
}
} if(flag)
{
u=v; continue;
} int Min=N;
for(int i=Adj2[u];~i;i=edge[i].next)
{
if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)
{
Min=dep[edge[i].to];
cur[u]=i;
}
}
gap[dep[u]]--;
if(!gap[dep[u]]) return ans;
dep[u]=Min+1;
gap[dep[u]]++;
if(u!=start) u=edge[pre[u]^1].to;
} return ans;
} void init()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
memset(Adj2,-1,sizeof(Adj2)); Size2=0;
for(int i=1;i<=n;i++) Pre[i].clear();
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i=0,u,v,c;i<m;i++)
{
scanf("%d%d%d",&u,&v,&c);
Add_Edge(u,v,c); Add_Edge(v,u,c);
}
spfa(edge,Adj);
rebuild();
int max_flow=sap(1,n,n);
int min_short_path=spfa(edge2,Adj2);
printf("%d %d\n",max_flow,m-min_short_path);
} return 0;
}

HDOJ 5294 Tricks Device 最短路(记录路径)+最小割的更多相关文章

  1. hdu 5294 Tricks Device 最短路建图+最小割

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294 Tricks Device Time Limit: 2000/1000 MS (Java/Other ...

  2. SPFA+Dinic HDOJ 5294 Tricks Device

    题目传送门 /* 题意:一无向图,问至少要割掉几条边破坏最短路,问最多能割掉几条边还能保持最短路 SPFA+Dinic:SPFA求最短路时,用cnt[i]记录到i最少要几条边,第二个答案是m - cn ...

  3. HDU 5294 Tricks Device 最短路+最大流

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5294 题意: 给你个无向图: 1.求最少删除几条边就能破坏节点1到节点n的最短路径, 2.最多能删除 ...

  4. HDU 5294 Tricks Device (最大流+最短路)

    题目链接:HDU 5294 Tricks Device 题意:n个点,m条边.而且一个人从1走到n仅仅会走1到n的最短路径.问至少破坏几条边使原图的最短路不存在.最多破坏几条边使原图的最短路劲仍存在 ...

  5. HDU 5294 Tricks Device(多校2015 最大流+最短路啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294 Problem Description Innocent Wu follows Dumb Zha ...

  6. HDU 5294 Tricks Device 网络流 最短路

    Tricks Device 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5294 Description Innocent Wu follows D ...

  7. hdu 5294 Tricks Device(2015多校第一场第7题)最大流+最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294   题意:给你n个墓室,m条路径,一个人在1号墓室(起点),另一个人在n号墓室(终点),起点的那 ...

  8. HDU 5294 Tricks Device (最短路,最大流)

    题意:给一个无向图(连通的),张在第n个点,吴在第1个点,‘吴’只能通过最短路才能到达‘张’,两个问题:(1)张最少毁掉多少条边后,吴不可到达张(2)吴在张毁掉最多多少条边后仍能到达张. 思路:注意是 ...

  9. hdu 4871 树的分治+最短路记录路径

    /* 题意:给你一些节点和一些边,求最短路径树上是k个节点的最长的路径数. 解:1.求出最短路径树--spfa加记录 2.树上进行操作--树的分治,分别处理子树进行补集等运算 */ #include& ...

随机推荐

  1. 王立平--split字符串切割

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzQyNTUyNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  2. [Angular] Getting to Know the @Attribute Decorator in Angular

    So when you using input binding in Angular, it will always check for update. If you want to improve ...

  3. SqlCommand.DeriveParameters failed

    错误信息例如以下: SqlCommand.DeriveParameters failed because the SqlCommand.CommandText property value is an ...

  4. JPEG压缩图像超分辨率重建算法

    压缩图像超分辨率重建算法学习 超分辨率重建是由一幅或多幅的低分辨率图像重构高分辨率图像,如由4幅1m分辨率的遥感图像重构分辨率0.25m分辨率图像.在军用/民用上都有非常大应用. 眼下的超分辨率重建方 ...

  5. 3D数学读书笔记——矩阵进阶

    本系列文章由birdlove1987编写,转载请注明出处. 文章链接:http://blog.csdn.net/zhurui_idea/article/details/25242725 最终要学习矩阵 ...

  6. Ant批量处理jmeter脚本

    Ant是一个可以把代码从某个地方拿来,编译,再拷贝到某个地方去的构建工具.一时冲动学习一下,顺便王婆卖瓜尝试着处理jmeter的脚本,于是,采坑之旅也从此开始.本文省略ant安装步骤和ant脚本说明, ...

  7. tcMalloc 配置和优化 nginx 高性能

    tcMalloc优化nginx  记住:nginx一定要先启动 1>下载安装libunwind: #wget  http://download.savannah.gnu.org/releases ...

  8. 内存问题检测神器:Valgrind

    Linux下内存问题检测神器:Valgrind 在写大型C/C++工程时难免会发生内存泄漏现象,系统编程中一个重要的方面就是有效地处理与内存相关的问题.你的工作越接近系统,你就需要面对越多的内存问题. ...

  9. Linux操作系统下Oracle主要监控工具介绍

    Oracle监控包括有效且完全地监控Oracle数据库的性能.可用性和使用率等统计量,还包括即时的错误通知和纠正措施,并提供全面的报表和图表.本文中主要介绍几种Linux操作系统下Oracle主要监控 ...

  10. POJ 1252 DP

    题意:给你6个数.让你求出1~100范围内的数 最优情况下由这六个数加减几步得到. 输出平均值和最大值. 思路: 我就随便写了写,,,感觉自己的思路完全不对. 但是交上去 AC了!!! 我先当减法 不 ...