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

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

题意:

给出n个墓室,m条路径。一个人在1号墓室(起点)。还有一个人在n号墓室(终点);

起点的那个人仅仅有通过最短路径才干追上终点的那个人。而终点的那个人能切断随意路径。

第一问——终点那人要使起点那人不能追上的情况下能够切的最少的路径数。输出最少的路径数

第二问——起点那人能追上终点那人的情况下,终点那人能切断的最多的路径数。输出最多的路径数

PS:

先跑最短路。

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

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

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

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

官方题解:

代码例如以下:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f #define MAXN 800047//点数的最大值
#define MAXM 2247//边数的最大值 int head[MAXM], pre[MAXM];
int dep[MAXM], cur[MAXM], gap[MAXM];//gap[x]=y :说明残留网络中dep[i]==x的个数为y
int EN;
struct Edge
{
int to,next,cap,flow;
} edge[MAXN]; //注意是MAXM
int tol;
int k, c, m;
int s, e;//源点。汇点
int map[MAXM][MAXM];
int cost1[MAXM][MAXM], cost2[MAXM][MAXM]; int num[MAXM][MAXM];//记录边数。>1 既有重边
//加边,单向图三个參数,双向图四个參数
void addedge(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;
while(dep[start] < N)
{
if(u == end)
{
int Min = INF;
int inser;
for(int 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(int 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(int 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(int 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;
} void Dijkstra(int s, int n, int cost[][MAXM], int dis[MAXM])
{
//int dis[MAXM];//记录到随意点的最短距离
int mark[MAXM];//记录被选中的结点
int i, j, k;
for(i = 1; i <= n; i++)
{
mark[i] = 0;//初始化全部结点。每一个结点都没有被选中
dis[i] = INF;
}
mark[s] = 1;//start结点被选中
dis[s] = 0;//将start结点的的距离设置为0
int MIN;//设置最短的距离。
for(i = 1; i <= n; i++)
{
k = 1;//赋初值非常重要
MIN = INF;
for(j = 1; j <= n; j++)
{
if(!mark[j] && dis[j] < MIN)//未被选中的结点中,距离最短的被选中
{
MIN = dis[j] ;
k = j;
}
}
mark[k] = 1;//标记为被选中
for(j = 1; j <= n; j++)
{
if(!mark[j] && dis[j]>dis[k] + cost[k][j])//改动剩余结点的最短距离
{
dis[j] = dis[k] + cost[k][j];
}
}
}
} void init()
{
memset(head,-1,sizeof(head));
memset(cost1,INF,sizeof(cost1));
memset(cost2,INF,sizeof(cost2));
memset(num,0,sizeof(num));
EN = 0;
} int main()
{
int n, m;
int u, v, w;
int dis1[MAXM], dis2[MAXM];
while(~scanf("%d%d",&n,&m))
{
init();
for(int i = 1; i <= n; i++)
{
cost1[i][i] = 0;
}
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d",&u,&v,&w);
if(cost1[u][v] > w)
{
cost1[u][v] = w;
cost1[v][u] = w;
num[u][v] = 1;
num[v][u] = 1;
}
else if(cost1[u][v] == w)//重边
{
num[u][v]++;
num[v][u]++;
}
}
Dijkstra(1,n,cost1,dis1);
for(int i = 1; i <= n; i++)
{
cost2[i][i] = 0;
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(i != j)
{
if(dis1[i] - dis1[j] == cost1[i][j])//最短路的边
{
cost2[j][i] = 1;
addedge(j,i,num[i][j]);
}
}
}
}
Dijkstra(1,n,cost2,dis2);
printf("%d %d\n",sap(1,n,n),m-dis2[n]);
}
return 0;
}

HDU 5294 Tricks Device(多校2015 最大流+最短路啊)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. SPFA+Dinic HDOJ 5294 Tricks Device

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

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

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

  9. HDU 5289 Assignment(多校2015 RMQ 单调(双端)队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 Problem Description Tom owns a company and he is ...

随机推荐

  1. BZOJ 3456 NTT图的计数 容斥

    思路: RT 懒得写了 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm&g ...

  2. 题解报告:hdu 1846 Brave Game(巴什博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1846 Problem Description 十年前读大学的时候,中国每年都要从国外引进一些电影大片, ...

  3. 笨拙而诡异的 Oracle

    有这样一段 SQL 代码: 通过 C# 获取查询结果:    SQL 代码中有两个参数,且都是字符串类型,以上的 C# 代码是生成 Oracle SQL 代码所需要的参数.运行结果如下:   居然发生 ...

  4. String字符串的完美度

    题目详情: 我们要给每个字母配一个1-26之间的整数,具体怎么分配由你决定,但不同字母的完美度不同, 而一个字符串的完美度等于它里面所有字母的完美度之和,且不在乎字母大小写,也就是说字母F和f的完美度 ...

  5. hibernate.cfg.xml配置

    hibernate.hbm2ddl.auto 配置: create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这 ...

  6. js基础---数据类型转换

    js中数据类型: 简单数据类型: number:233,-34,0x23,023 string:"hello"或者'hello' boolean:true.false undefi ...

  7. CSS——新浪导航demo

    主要运用的dispaly将a变成行内块,再用padding撑开宽度. <!DOCTYPE html> <html lang="en"> <head&g ...

  8. 基于证书的MS SQL2005数据库镜像搭建

    一.准备工作: 3台服务器同版本,硬盘分区大小相同,安装相同版本数据库软件. host中分别标注3台服务器IP和主机名称. 主体服务器上创建数据库,并进行完整备份数据库和数据库事务. 拷贝备份文件给镜 ...

  9. 12--c完数/最大公约数/最小公倍数/素数/回文数

    完数/最大公约数/最小公倍数/素数/回文数 2015-04-08 10:33 296人阅读 评论(0) 收藏 举报  分类: C/C++(60)  哈尔滨工业大学(8)  版权声明:本文为博主原创文章 ...

  10. Eclipse安装egit Github教程

    网址:http://download.eclipse.org/egit/updates 教程: http://jingyan.baidu.com/article/4853e1e529483c1909f ...