HDU 5294 Tricks Device (最短路,最大流)
题意:给一个无向图(连通的),张在第n个点,吴在第1个点,‘吴’只能通过最短路才能到达‘张’,两个问题:(1)张最少毁掉多少条边后,吴不可到达张(2)吴在张毁掉最多多少条边后仍能到达张。
思路:注意是最短路才可达,但是最短路径可能有多条(即权值相等的)!!
第二个问题好回答,来次最短路,记录下到达每个点在最低权值的情况下的最少次用边。
第一个问题,同样只要砍掉最短路的某些边即可。要根据第2个问题所跑的SSSP,将不是最短路的边的剔除,重新建图,跑最大流,得到结果。
当然要考虑重边!
#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=;
vector<int> vect[], vect2[];
int g[][];
struct node
{
int from,to,cap,flow;
node(){};
node(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow){};
}edge[N*];
struct node2//跑最短路用的
{
int from,to,cost;
node2(){};
node2(int from,int to,int cost):from(from),to(to),cost(cost){};
}oldedge[N*]; int edge_cnt,edge_cnt2, n, m;
void add_node(int from,int to,int cap,int flow)
{
edge[edge_cnt]=node(from, to, cap, flow);
vect[from].push_back(edge_cnt++);
}
void add_node2(int from,int to,int cost)//跑最短路用的
{
oldedge[edge_cnt2]=node2(from, to, cost);
vect2[from].push_back(edge_cnt2++);
} int flow[], path[];
int cost[], inq[], times[]; int BFS(int s,int e)
{
deque<int> que(,s);
flow[s]=INF;
while(!que.empty())
{
int x=que.front();
que.pop_front();
for(int i=; i<vect[x].size(); i++)
{
node e=edge[vect[x][i]];
if(!flow[e.to] && e.cap>e.flow)
{
flow[e.to]=min(flow[e.from], e.cap-e.flow);
path[e.to]=vect[x][i];
que.push_back(e.to);
}
}
if(flow[e]) return flow[e];
}
return flow[e];
}
int max_flow(int s,int e)
{
int ans_flow=;
while(true)
{
memset(path,,sizeof(path));
memset(flow,,sizeof(flow)); int tmp=BFS(s,e);
if(!tmp) return ans_flow;
ans_flow+=tmp; int ed=e;
while(ed!=s)
{
int t=path[ed];
edge[t].flow+=tmp;
edge[t^].flow-=tmp;
ed=edge[t].from;
}
}
} int spfa(int s,int e)
{
memset(cost,0x7f,sizeof(cost));
memset(inq,,sizeof(inq));
memset(times,0x7f,sizeof(times));//记录到达每个点的最少用边,前提是权最少 deque<int> que(,s);
cost[s]=;
times[s]=;
while(!que.empty())
{
int x=que.front();que.pop_front();
inq[x]=;
for(int i=; i<vect2[x].size(); i++)
{
node2 e=oldedge[vect2[x][i]];
if(cost[e.to]>=cost[e.from]+e.cost)
{
if( cost[e.to]>cost[e.from]+e.cost) times[e.to]=times[e.from]+;
else times[e.to]=min(times[e.to], times[e.from]+); cost[e.to]=cost[e.from]+e.cost;
if(!inq[e.to])
{
inq[e.to]=;
que.push_back(e.to);
}
}
}
}
return times[e];
} void build_graph()
{
for(int i=; i<=n; i++)
{
for(int j=; j<vect2[i].size(); j++)
{
node2 e=oldedge[vect2[i][j]];
if(cost[e.to]==cost[e.from]+e.cost)
{
add_node(e.from,e.to,,);
add_node(e.to,e.from,,);
}
}
}
} int main()
{
freopen("input.txt", "r", stdin);
int a, b, c;
while(~scanf("%d%d", &n, &m))
{
edge_cnt=;
edge_cnt2=;
memset(edge,,sizeof(edge));
memset(oldedge,,sizeof(oldedge));
for(int i=n; i>=; i--) vect[i].clear(),vect2[i].clear(); for(int i=; i<m; i++)
{
scanf("%d %d %d", &a, &b, &c);
add_node2(a,b,c);
add_node2(b,a,c);
}
int ans2=spfa(,n);
build_graph();//重新建图
int ans1=max_flow(,n);
printf("%d %d\n", ans1, m-ans2);
}
return ;
}
AC代码
HDU 5294 Tricks Device (最短路,最大流)的更多相关文章
- HDU 5294 Tricks Device 最短路+最大流
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5294 题意: 给你个无向图: 1.求最少删除几条边就能破坏节点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 ...
- HDU 5294 Tricks Device (最大流+最短路)
题目链接:HDU 5294 Tricks Device 题意:n个点,m条边.而且一个人从1走到n仅仅会走1到n的最短路径.问至少破坏几条边使原图的最短路不存在.最多破坏几条边使原图的最短路劲仍存在 ...
- HDU 5294 Tricks Device 网络流 最短路
Tricks Device 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5294 Description Innocent Wu follows D ...
- HDOJ 5294 Tricks Device 最短路(记录路径)+最小割
最短路记录路径,同一时候求出最短的路径上最少要有多少条边, 然后用在最短路上的边又一次构图后求最小割. Tricks Device Time Limit: 2000/1000 MS (Java/Oth ...
- 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(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 ...
- Tricks Device (hdu 5294 最短路+最大流)
Tricks Device Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
随机推荐
- python学习之“切片操作从入门到精通”
在python学习开发的过程中,我们总是不断的要对List(列表),Tuple(元组)有取值操作:假如我们有一个列表List1现在想取出1其中的前5个元素,改怎么操作呢? >>> L ...
- 安装 SQL SERVER PROFILER
SQL SERVER 2008 R2 (10.50.40) 版本,安装 SQL SERVER PROFILER:通过 command prompt,使用以下命令:setup.exe /FEATURES ...
- JsRender系列-11
<!DOCTYPE html> <html> <head> <script type="text/javascript" src=&quo ...
- 内存对齐-C语言struct内存占用问题
转1个写的比较全面的. http://hubingforever.blog.163.com/blog/static/17104057920122256134681/ 本文编辑整理自:http://hi ...
- hadoop:could only be replicated to 0 nodes, instead of 1
在Hadoop的环境搭建过程中,常常会遇到类似这样的错误信息提示:“could only be replicated to 0 nodes, instead of 1 ”,产生这样的错误原因有多种,这 ...
- lintcode :数组剔除元素后的乘积
题目: 数组剔除元素后的乘积 给定一个整数数组A. 定义B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], 计算B的时候请不要使用除法. 样例 给出 ...
- java socket知识点
3.用线程池实现TCP服务器端时,首先创建一个ServerSocket实例,然后创建N个线程,每个线程反复循环,从(共享的)ServerSocket实例接收客户端连接.当多个线程同时调用一个Serve ...
- 开源入侵检测系统OSSEC搭建之三:Web界面安装
注意:以下操作需在OSSEC服务端进行设置 一.下载analogi,存放于/var/www/html/下并赋予权限 [root@localhost ~]# wget https://github.co ...
- jQuery 、js 设置 显示隐藏
小问题 jQuery 操作方式: $("#ddl").parent().attr("style", "display:none"); j ...
- javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint
使用hibernate validator出现上面的错误, 需要 注意 @NotNull 和 @NotEmpty 和@NotBlank 区别 @NotEmpty 用在集合类上面@NotBlank 用 ...