Tricks Device

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1977    Accepted Submission(s): 509

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条边,构建有权无向图。

求出删去最少条边数可以使得图没有最短路径,以及删出最多条边使得图仍有最多条路径。

思路:

最短路处理出最短路径图,做法是使用dis数组,若若dis[v]-dis[u] = w(u,v),则该路在最短路径中。

建出最短路径之后 跑一次网络流,得到第一个答案。

在跑最短路中记录最短路的最少路数,ans2 = m - minb.

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
using namespace std;
const int N=2005;
const int MAXN=(1<<31)-1;
int INF=0x7f7f7f7f;
int T,n,m,k,tot;
int cas=1;
int head[N];
struct Edge{
int to,w,next;
}edge[60005*2];
void addedge(int u,int v,int w){
edge[tot].to=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++; edge[tot].to=u;
edge[tot].w=w;
edge[tot].next=head[v];
head[v]=tot++;
} int dis[N],vis[N];
int minb[N];
int spfa(int s){
memset(dis,0x3f,sizeof dis);
memset(vis,0,sizeof vis);
memset(minb,0x3f,sizeof minb); queue<int> q;
dis[s]=0;
minb[s]=0;
vis[s]=1;
q.push(s); while(!q.empty()){
int u=q.front();q.pop();
vis[u]=0; for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to,w=edge[i].w;
if(dis[v]==dis[u]+w){
minb[v]=min(minb[v],minb[u]+1);
if(!vis[v]){
vis[v]=1;
q.push(v);
}
} if(dis[v]>dis[u]+w){
dis[v]=dis[u]+w;
minb[v]=minb[u]+1;
if(!vis[v]){
vis[v]=1;
q.push(v);
}
} }
}
}
struct Eg{
int u,cap,rev;
Eg(int uu,int cc,int rr){
u=uu;cap=cc;rev=rr;
}
};
vector<Eg> G[N];
void add(int u,int v,int cap){
G[u].push_back(Eg(v,cap,G[v].size()));
G[v].push_back(Eg(u,0,G[u].size()-1));
} void build(){
for(int i=1;i<=n;i++){
for(int j=head[i];~j;j=edge[j].next){
int v=edge[j].to,w=edge[j].w;
if(dis[v]==dis[i]+w){
add(i,v,1); }
}
}
}
bool used[N];
int dfs(int v,int t,int f){
if(v==t) return f;
used[v]=true;
for(int i=0;i<G[v].size();i++){
Eg &e=G[v][i];
if(!used[e.u] && e.cap>0){
int d=dfs(e.u,t,min(f,e.cap));
if(d>0){
e.cap-=d;
G[e.u][e.rev].cap+=d;
return d;
}
}
}
return 0;
}
int max_flow(int s,int t){
int flow=0;
while(1){
memset(used,0,sizeof used);
int f=dfs(s,t,INF);
if(f==0) return flow;
flow+=f;
}
}
void init(){
tot=0;
memset(head,-1,sizeof head);
for(int i=0;i<N;i++) G[i].clear();
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aaa","r",stdin);
#endif while(~scanf("%d%d",&n,&m)){
init();
for(int i=0;i<m;i++){
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
addedge(u,v,w);
}
spfa(1); build();
int ans=max_flow(1,n);
printf("%d %d\n",ans,m-minb[n]);
} return 0;
}

  

2015 多校联赛 ——HDU5294(最短路,最小切割)的更多相关文章

  1. 2015 多校联赛 ——HDU5301(技巧)

    Your current task is to make a ground plan for a residential building located in HZXJHS. So you must ...

  2. 2015 多校联赛 ——HDU5349(水)

    Problem Description A simple problem Problem Description You have a multiple set,and now there are t ...

  3. 2015 多校联赛 ——HDU5334(构造)

    Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  4. 2015 多校联赛 ——HDU5335(Walk out)

    Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total S ...

  5. 2015 多校联赛 ——HDU5302(构造)

    Connect the Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. 2015 多校联赛 ——HDU5325(DFS)

    Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Tota ...

  7. 2015 多校联赛 ——HDU5316(线段树)

    Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...

  8. 2015 多校联赛 ——HDU5323(搜索)

    Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  9. 2015 多校联赛 ——HDU5319(模拟)

    Painter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

随机推荐

  1. 团队作业7——第二次项目冲刺(Beta版本12.06)

    项目每个成员的进展.存在问题.接下来两天的安排. 已完成的内容:队员每个人提出对接下来需要做的事情的看法和意见,将需要做的任务更新到了leangoo中进行管理,产品完成了界面优化的设计,测试复现了之前 ...

  2. 201621123031 《Java程序设计》第8周学习总结

    作业08-集合 1.本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2.书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码 ...

  3. Python 单向循环链表

    操作 is_empty() 判断链表是否为空 length() 返回链表的长度 travel() 遍历 add(item) 在头部添加一个节点 append(item) 在尾部添加一个节点 inser ...

  4. 使用genstring和NSLocalizedString实现App文本的本地化

    OS提供了简便的方法来实现本地化,其中用的最多的就是NSLocalizedString. 首先查看下NSLocalizedString是什么: #define NSLocalizedString(ke ...

  5. Java如何调取创蓝253短信验证码

    基于创蓝253短信服务平台的Java调用短信接口API package com.bcloud.msg.http; import java.io.ByteArrayOutputStream; impor ...

  6. nyoj 聪明的kk

    聪明的kk 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 聪明的"KK"非洲某国展馆的设计灵感源于富有传奇色彩的沙漠中陡然起伏的沙丘,体现出本国 ...

  7. 利用Node的chokidar 监听文件改变的文件。

    最近维护一个项目.每次改完东西,都要上传到服务器.然后有时候就忘记一些东西,于是就想有没有可以方法能监听文件的改变.然后我再利用程序把更改的文件一键上传到服务器. 于是就找到了nodejs 的chok ...

  8. unity A*寻路 (三)A*算法

    这里我就不解释A*算法 如果你还不知道A*算法 网上有很多简单易懂的例子 我发几个我看过的链接 http://www.cnblogs.com/lipan/archive/2010/07/01/1769 ...

  9. python入门(13)获取函数帮助和调用函数

    Python内置了很多有用的函数,我们可以直接调用. 要调用一个函数,需要知道函数的名称和参数,比如求绝对值的函数abs,只有一个参数.可以直接从Python的官方网站查看文档: http://doc ...

  10. javascript实现继承3种方式: 原型继承、借用构造函数继承、组合继承,模拟extends方法继承

    javascript中实现继承的三种方式:原型继承.借用构造函数继承.混合继承: /* js当中的继承 js中 构造函数 原型对象 实力对象的关系: 1 构造函数.prototype = 原型对象 2 ...