Tricks Device (hdu 5294 最短路+最大流)
Tricks Device
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 124 Accepted Submission(s): 27
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.
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.
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
2 6
pid=5298" target="_blank">5298
pid=5297" target="_blank">5297
5296pid=5295" target="_blank">5295
题意:n个点m条无向边,如果从起点0到终点n-1的最短路距离为dist,求最少删除多少条边使得图中不再存在最短路。最多删除多少条边使得图中仍然存在最短路。
思路:先用spfa求一次最短路,开一个road数组,road[i]表示从起点走到i点最短路径所经过的最少边数,然后第二问就是m-road[n-1];再依据最短路的dist数组推断哪些边是最短路上的,用它们又一次构图。跑一遍网络流求最小割。比赛的时候没有在最短路上建边,直接用的原图。果断TLE,又坑了队友=-=
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std; #define INF 0x3f3f3f3f
#define mod 1000000009
const int MAXN = 2005;
const int MAXM = 200005;
const int N = 1005; int n,m; struct EDGE
{
int u,v,len,next;
}e[MAXM]; struct Edge
{
int to,next,cap,flow;
}edge[MAXM]; int tol;
int head[MAXN]; void init()
{
tol=0;
memset(head,-1,sizeof(head));
} void add(int u,int v,int len)
{
e[tol].u=u;
e[tol].v=v;
e[tol].len=len;
e[tol].next=head[u];
head[u]=tol++;
e[tol].u=v;
e[tol].v=u;
e[tol].len=len;
e[tol].next=head[v];
head[v]=tol++;
} 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];
int dep[MAXN],cur[MAXN],sta[MAXN]; bool bfs(int s,int t,int n)
{
int front=0,tail=0;
memset(dep,-1,sizeof(dep[0])*(n+1));
dep[s]=0;
Q[tail++]=s;
while (front<tail)
{
int u=Q[front++];
for (int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if (edge[i].cap>edge[i].flow && dep[v]==-1)
{
dep[v]=dep[u]+1;
if (v==t) return true;
Q[tail++]=v;
}
}
}
return false;
} int dinic(int s,int t,int n)
{
int maxflow=0;
while (bfs(s,t,n))
{
for (int i=0;i<n;i++) cur[i]=head[i];
int u=s,tail=0;
while (cur[s]!=-1)
{
if (u==t)
{
int tp=INF;
for (int i=tail-1;i>=0;i--)
tp=min(tp,edge[sta[i]].cap-edge[sta[i]].flow);
maxflow+=tp;
for (int i=tail-1;i>=0;i--)
{
edge[sta[i]].flow+=tp;
edge[sta[i]^1].flow-=tp;
if (edge[sta[i]].cap-edge[sta[i]].flow==0)
tail=i;
}
u=edge[sta[tail]^1].to;
}
else if (cur[u]!=-1 && edge[cur[u]].cap > edge[cur[u]].flow &&dep[u]+1==dep[edge[cur[u]].to])
{
sta[tail++]=cur[u];
u=edge[cur[u]].to;
}
else
{
while (u!=s && cur[u]==-1)
u=edge[sta[--tail]^1].to;
cur[u]=edge[cur[u]].next;
}
}
}
return maxflow;
} int dist[MAXN];
int vis[MAXN];
int road[MAXN]; void SPFA()
{
memset(vis,0,sizeof(vis));
memset(dist,INF,sizeof(dist));
memset(road,INF,sizeof(road));
dist[0]=0;
road[0]=0;
vis[0]=1;
queue<int>Q;
Q.push(0);
while (!Q.empty())
{
int u=Q.front();
Q.pop();
vis[u]=0;
for (int i=head[u];~i;i=e[i].next)
{
int v=e[i].v;
if (dist[v]>dist[u]+e[i].len)
{
dist[v]=dist[u]+e[i].len;
road[v]=road[u]+1;
if (!vis[v])
{
vis[v]=1;
Q.push(v);
}
}
else if (dist[v]==dist[u]+e[i].len)
{
if (road[v]>road[u]+1)
{
road[v]=road[u]+1;
if (!vis[v])
{
vis[v]=1;
Q.push(v);
}
}
}
}
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
int i,j,u,v,w;
while (~sff(n,m))
{
init();
for (i=0;i<m;i++)
{
sfff(u,v,w);
if (u==v) continue;
u--;v--;
add(u,v,w);
}
SPFA();
int cnt=tol;
init();
for (i=0;i<cnt;i++)
{
u=e[i].u;
v=e[i].v;
if (dist[v]==dist[u]+e[i].len)
addedge(u,v,1);
}
int ans=dinic(0,n-1,n);
pf("%d %d\n",ans,m-road[n-1]);
}
return 0;
}
Tricks Device (hdu 5294 最短路+最大流)的更多相关文章
- hdu 5294 最短路+最大流 ***
处理处最短路径图,这个比较巧妙 链接:点我
- HDU 5294 Tricks Device(多校2015 最大流+最短路啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294 Problem Description Innocent Wu follows Dumb Zha ...
- hdu 3599(最短路+最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3599 思路:首先spfa求一下最短路,然后对于满足最短路上的边(dist[v]==dist[u]+w) ...
- 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 网络流 最短路
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 ...
- SPFA+Dinic HDOJ 5294 Tricks Device
题目传送门 /* 题意:一无向图,问至少要割掉几条边破坏最短路,问最多能割掉几条边还能保持最短路 SPFA+Dinic:SPFA求最短路时,用cnt[i]记录到i最少要几条边,第二个答案是m - cn ...
- HDU5294 Tricks Device(最大流+SPFA) 2015 Multi-University Training Contest 1
Tricks Device Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
随机推荐
- Java盲点:双重检查锁定及单例模式
尊重原创: http://gstarwd.iteye.com/blog/692937 2004 年 5 月 01 日 所有的编程语言都有一些共用的习语.了解和使用一些习语很有用,程序员们花费宝贵的时间 ...
- java之内部类与匿名内部类
Java 内部类 分四种:成员内部类.局部内部类.静态内部类和匿名内部类. 1.成员内部类: 即作为外部类的一个成员存在,与外部类的属性.方法并列. 注意:成员内部类中不能定义静态变量,但可以访问外部 ...
- 【 D3.js 高级系列 — 5.1 】 颜色插值和线性渐变
颜色插值指的是给出两个 RGB 颜色值,两个颜色之间的值通过插值函数计算得到.线性渐变是添加到 SVG 图形上的过滤器,只需给出两端的颜色值即可. 1. 颜色插值 在[高级 - 第 5.0 章]里已经 ...
- 【转】java枚举类型enum的使用
原文网址:http://blog.csdn.net/wgw335363240/article/details/6359614 java 枚举类型enum 的使用 最近跟同事讨论问题的时候,突然同事提到 ...
- 如何编写Linux设备驱动程序
一.Linux device driver 的概念 系统调用是操作系统内核和应用程序之间的接口,设备驱动程序是操作系统内核和机器硬件之间的接口.设备驱动程序为应用程序屏蔽了硬件的细节,这样在应用程序看 ...
- Spring @Resource、@Autowired、@Qualifier的注解注入及区别
spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖.在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入 ...
- HDU 1520-Anniversary party(树形dp入门)
题意: n个人参加party,已知每人的欢乐值,给出n个人的工作关系树,一个人和他的顶头上司不能同时参加,party达到的最大欢乐值. 分析:dp[i][f],以i为根的子树,f=0,i不参加,f=1 ...
- 那些年一起踩过的坑 — java 自动装箱拆箱问题
坑在哪里? 我们都知道Java的八种基本数据类型:int, short, long, double, byte, char, float, boolean 分别有各自对应的包装类型:Integ ...
- 山东省2016acm省赛
A 水 #include <iostream> #include <cstdio> #include <algorithm> #include <list&g ...
- C++ 我想这样用(一)
虽然还是菜鸟,但我是一个地地道道的c程序员,甚至一度很讨厌C++(虽然现在也是). 为了在不用C++的情况下学习和使用面向对象而长期奔走,曾经用过一年的Python,后终放弃.之后很长一段时间里摆弄O ...