链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294

Tricks Device

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

Total Submission(s): 375    Accepted Submission(s): 98

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
 

题意:

1到n点有最短路。

问你 最少破坏多少条路会阻断全部最短路。

再问你 最多破坏多少边,仍有和原始图的最短路一样长度的最短路存在。

做法:

先跑最短路。

然后通过  dist[i]-dist[j] == map[j][i]

假设符合的话  map[j][i]就是 最短路中的一条边。

然后把这些最短路的边 建图,跑最大流,流量是有多少边权同样的重边,跑出来就是最小割。也就是阻断全部最短路的最小花费。花费是每破坏一条路为1。

所以出来的值。就是破坏了多少的边。

然后如最大流相同的建边,跑最短路,边权为1。跑出来的最短路dist[n]。就是  跨越边数最少的 最短路的边数了。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <string.h>
using namespace std; const int MAXN = 2200;//点数的最大值
const int MAXM = 800000;//边数的最大值
const int INF2 = 2000000000;
struct Edge1
{
int to,next,cap,flow;
}edge[MAXM];//注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],cur[MAXN];
void init()
{
tol = 0;
memset(head,-1,sizeof (head));
}
void add (int u,int v,int w,int rw = 0)//网络流要有反向弧
{
edge[tol].to = v; edge[tol].cap = w; edge[tol].flow = 0;
edge[tol].next = head[u]; head[u] = tol++;
edge[tol].to = u; edge[tol].cap = rw; edge[tol].flow = 0;
edge[tol].next = head[v]; head[v] = tol++;
}
int Q[MAXN];
void BFS(int start,int end)
{
memset(dep,-1,sizeof(dep));
memset(gap,0,sizeof(gap));
gap[0] = 1;
int front = 0, rear = 0;
dep[end] = 0;
Q[rear++] = end;
while(front != rear)
{
int u = Q[front++];
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i]. to;
if(dep[v] != -1)continue;
Q[rear++] = v;
dep[v] = dep[u] + 1;
gap[dep[v]]++;
}
}
}
int S[MAXN];
int sap(int start,int end, int N)//有几个点
{
BFS(start,end);
memcpy(cur,head,sizeof(head));
int top = 0;
int u = start;
int ans = 0;
int i;
while(dep[start] < N)
{
if(u == end)
{
int Min = INF2;
int inser;
for( i = 0;i < top;i++)
{
if(Min > edge[S[i]].cap - edge[S[i]].flow)
{
Min = edge[S[i]].cap - edge[S[i]].flow;
inser = i;
}
}
for( i = 0;i < top;i++)
{
edge[S[i]]. flow += Min;
edge[S[i]^1].flow -= Min;
}
ans += Min;
top = inser;
u = edge[S[top]^1].to;
continue;
}
bool flag = false;
int v;
for( i = cur[u]; i != -1; i = edge[i]. next)
{
v = edge[i]. to;
if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u])
{
flag = true;
cur[u] = i;
break;
}
}
if(flag)
{
S[top++] = cur[u];
u = v;
continue;
}
int Min = N;
for( i = head[u]; i != -1; 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[S[--top]^1].to;
}
return ans;
} #define typec int
typec INF=0x3f3f3f3f;//防止后面溢出。这个不能太大
bool vis[MAXN];
int pre[MAXN];
void Dijkstra(typec cost[][MAXN],typec lowcost[],int n,int beg)
{
for(int i=0;i<n;i++)
{
lowcost[i]=INF;
vis[i]=false;
pre[i]=-1;
}
lowcost[beg]=0;
for(int j=0;j<n;j++)
{
int k=-1;
int Min=INF;
for(int i=0;i<n;i++)
if(!vis[i]&&lowcost[i]<Min)
{
Min=lowcost[i];
k=i;
}
if(k==-1)break;
vis[k]=true;
for(int i=0;i<n;i++)
if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i])
{
lowcost[i]=lowcost[k]+cost[k][i];
pre[i]=k;
}
}
}
int cost2[MAXN][MAXN];
int dist2[MAXN]; int cost[MAXN][MAXN];
int num[MAXN][MAXN];
int has[MAXN];
int dist[MAXN];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
memset(cost,INF,sizeof cost);
memset(num,0,sizeof num);
for(int i=0;i<n;i++)
cost[i][i]=0; for(int i=1;i<=m;i++)//双向 有重边
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
u--;
v--;
if(cost[u][v]>w)
{
cost[u][v]=w;
cost[v][u]=w;
num[u][v]=1;
num[v][u]=1;
}
else if(cost[u][v]==w)
{
num[u][v]++;
num[v][u]++;
} }
Dijkstra(cost,dist,n,0);// wuxiangda bulian memset(cost2,INF,sizeof cost2);
for(int i=0;i<n;i++)
cost2[i][i]=0; for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i!=j)
if(dist[i]-dist[j]==cost[i][j])
{
cost2[j][i]=1;
add(j,i,num[i][j]);
}
}
}
Dijkstra(cost2,dist2,n,0);
printf("%d %d\n",sap(0,n-1,n),m-dist2[n-1]);
}
return 0;
}

hdu 5294 Tricks Device 最短路建图+最小割的更多相关文章

  1. HDOJ 5294 Tricks Device 最短路(记录路径)+最小割

    最短路记录路径,同一时候求出最短的路径上最少要有多少条边, 然后用在最短路上的边又一次构图后求最小割. Tricks Device Time Limit: 2000/1000 MS (Java/Oth ...

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

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

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

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

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

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

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

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

  6. SPOJ-OPTM Optimal Marks ★★(按位建图 && 最小割)

    [题意]给出一个无向图,每个点有一个标号mark[i],不同点可能有相同的标号.对于一条边(u, v),它的权值定义为mark[u] xor mark[v].现在一些点的标号已定,请决定剩下点的标号, ...

  7. bzoj 1001 平面图转对偶图 最短路求图最小割

    原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1001 整理了下之前A的题 平面图可以转化成对偶图,然后(NlogN)的可以求出图的最小割( ...

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

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

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

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

随机推荐

  1. 详解cookie与session的区别,讲得最透彻的一篇文章

    在PHP面试中 经常碰到请阐述session与cookie的区别与联系,以及如何修改两者的有效时间. 大家都知道,session是存储在服务器端的,cookie是存储在客户端的,session依赖于c ...

  2. (九)expect批量公钥推送

    (1)expect实现ssh非交互登录 注意:注释不能出现这脚本里面 spawn表示开启一个会话 \r:表示回车,exp_continue :表示没有出现这样,继续往下执行 interact :停留在 ...

  3. python 2 控制台传参,解包,编码问题初探

    python 2 控制台传参,需要从sys模块中导入argv,argv返回的第一个参数当前脚本(script)的文件名,后面是参数,参数个数必须和解包(unpack)时使用的参数个数一致 1.本例子演 ...

  4. [Contest20180316]Mythological IV

    令$S(n)=\sum\limits_{i=0}^{n-1}f(i)q^i$,那么存在一个次数$\leq k$的多项式使得$S(n)=q^ng(n)-g(0)$(证明来自杜教的PPT) 设$f$的次数 ...

  5. 【强连通分量缩点】【记忆化搜索】bzoj1589 [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果

    缩成DAG f(i)表示以i为起点的最长路 #include<cstdio> #include<cstring> #include<algorithm> #incl ...

  6. [Gym101194G][CHINA-Final2016]Pandaria

    题目大意: 给你一个$n(n\le10^5)$个点,$m(m\le2\times10^5)$条边的无向图,每个点有一个颜色$c_i$,每条边有一个边权$w_i$.$q(q\le2\times10^5) ...

  7. MR实现--矩阵乘法

    import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.io ...

  8. 动态规划之DP中判断是否到达某一状态(最短时间是什么)?

    codevs1684 垃圾陷阱  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold   题目描述 Description 卡门——农夫约翰极其珍视的一条Holste ...

  9. 理解面向对象编程---C#控制台实现52张扑克牌的分法

    52张牌随机分给4个玩家,要求每个玩家的牌用一个一维数组表示. 我们采用模拟大法.初始化一副扑克牌,洗牌,发牌. using System; using System.Collections.Gene ...

  10. 引用日志log4net.dll的web.config配置

    <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigu ...