Tricks Device

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

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
 
解题:最短路+最小割
 
先把所有的最短路提取到另一份图中,然后看看最少经过几条边(可以用dp优化,或者标记已经访问的边来加速)可以由终点到起点
m-减去最少的可经过的边 即可删除的边
 
然后再对刚才提取的图 求最小割,最小割即为最少删除几条边,可以使得最短路变长
 
需要注意重边的影响
 
 
 #include <bits/stdc++.h>
#define pii pair<int,int>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
struct arc {
int to,w,next,id;
arc(int x = ,int y = ,int z = -) {
to = x;
w = y;
next = z;
}
} e[maxn];
int d[maxn],tot,S,T,head[maxn],cur[maxn];
vector< pii >g[maxn];
void add(int u,int v,int wa,int wb,int id = ) {
e[tot] = arc(v,wa,head[u]);
e[tot].id = id;
head[u] = tot++;
e[tot] = arc(u,wb,head[v]);
e[tot].id = id;
head[v] = tot++;
}
bool done[maxn];
priority_queue< pii,vector< pii >,greater< pii > >q;
void dijkstra() {
while(!q.empty()) q.pop();
memset(d,0x3f,sizeof d);
d[S] = ;
memset(done,false,sizeof done);
q.push(pii(d[S],S));
while(!q.empty()) {
int u = q.top().second;
q.pop();
if(done[u]) continue;
done[u] = true;
for(int i = head[u]; ~i; i = e[i].next) {
if(d[e[i].to] > d[u] + e[i].w) {
d[e[i].to] = d[u] + e[i].w;
g[e[i].to].clear();
g[e[i].to].push_back(pii(u,e[i].id));
q.push(pii(d[e[i].to],e[i].to));
} else if(d[e[i].to] == d[u]+e[i].w) {
g[e[i].to].push_back(pii(u,e[i].id));
q.push(pii(d[e[i].to],e[i].to));
}
}
}
}
int minstep;
void dfs(int u,int dep,int fa) {
if(u == S) {
minstep = min(dep,minstep);
return;
}
for(int i = g[u].size()-; i >= ; --i) {
if(g[u][i].first == fa) continue;
dfs(g[u][i].first,dep+,u);
bool flag = true;
for(int j = head[g[u][i].first]; flag && ~j; j = e[j].next) {
if(e[j].id == g[u][i].second) flag = false;
}
if(flag) {
add(g[u][i].first,u,,,g[u][i].second);
//cout<<g[u][i]<<" *** "<<u<<endl;
}
}
}
bool bfs() {
queue<int>q;
memset(d,-,sizeof d);
d[S] = ;
q.push(S);
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].w && d[e[i].to] == -) {
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[T] > -;
}
int dfs(int u,int low) {
if(u == T) return low;
int tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next) {
if(e[i].w &&d[e[i].to] == d[u]+&&(a=dfs(e[i].to,min(e[i].w,low)))) {
e[i].w -= a;
e[i^].w += a;
low -= a;
tmp += a;
if(!low) break;
}
}
if(!tmp) d[u] = -;
return tmp;
}
int dinic() {
int ret = ;
while(bfs()) {
memcpy(cur,head,sizeof head);
ret += dfs(S,INF);
}
return ret;
}
int main() {
int n,m,u,v,w;
while(~scanf("%d%d",&n,&m)) {
for(int i = tot = ; i < maxn; ++i) {
g[i].clear();
head[i] = -;
}
for(int i = ; i < m; ++i) {
scanf("%d%d%d",&u,&v,&w);
add(u,v,w,w,i);
}
S = ;
T = n;
dijkstra();
minstep = INT_MAX;
memset(head,-,sizeof head);
tot = ;
dfs(T,,-);
int by = m-minstep;
int ax = dinic();
printf("%d %d\n",ax,by);
}
return ;
}

重新写了下,思路更清楚些

 #include <bits/stdc++.h>
using namespace std;
using PII = pair<int,int>;
const int maxn = ;
const int INF = 0x3f3f3f3f;
struct arc {
int to,w,next;
arc(int x = ,int y = ,int z = -) {
to = x;
w = y;
next = z;
}
} e[];
int head[maxn],hd[maxn],tot,S,T,n,m;
int gap[maxn],d[maxn];
bool done[maxn];
void add(int head[maxn],int u,int v,int wa,int wb) {
e[tot] = arc(v,wa,head[u]);
head[u] = tot++;
e[tot] = arc(u,wb,head[v]);
head[v] = tot++;
}
void dijkstra() {
memset(d,0x3f,sizeof d);
memset(done,false,sizeof done);
priority_queue<PII,vector<PII>,greater<PII>>q;
d[S] = ;
q.push(PII(,S));
while(!q.empty()) {
int u = q.top().second;
q.pop();
if(done[u]) continue;
done[u] = true;
for(int i = hd[u]; ~i; i = e[i].next) {
if(d[e[i].to] > d[u] + e[i].w) {
d[e[i].to] = d[u] + e[i].w;
q.push(PII(d[e[i].to],e[i].to));
}
}
}
}
void build() {
for(int i = ; i <= n; ++i) {
for(int j = hd[i]; ~j; j = e[j].next) {
if(d[e[j].to] == d[i] + e[j].w)
add(head,i,e[j].to,,);
}
}
}
int bfs() {
memset(gap,,sizeof gap);
memset(d,-,sizeof d);
queue<int>q;
d[T] = ;
q.push(T);
while(!q.empty()) {
int u = q.front();
q.pop();
++gap[d[u]];
for(int i = head[u]; ~i; i = e[i].next) {
if(d[e[i].to] == -) {
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[S];
}
int dfs(int u,int low) {
if(u == T) return low;
int tmp = ,minH = n - ;
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].w) {
if(d[e[i].to] + == d[u]) {
int a = dfs(e[i].to,min(e[i].w,low));
e[i].w -= a;
e[i^].w += a;
tmp += a;
low -= a;
if(!low) break;
if(d[S] >= n) return tmp;
}
if(e[i].w) minH = min(minH,d[e[i].to]);
}
}
if(!tmp) {
if(--gap[d[u]] == ) d[S] = n;
++gap[d[u] = minH + ];
}
return tmp;
}
int sap(int ret = ) {
while(d[S] < n) ret += dfs(S,INF);
return ret;
}
int main() {
int u,v,w;
while(~scanf("%d%d",&n,&m)) {
memset(head,-,sizeof head);
memset(hd,-,sizeof hd);
for(int i = tot = ; i < m; ++i) {
scanf("%d%d%d",&u,&v,&w);
add(hd,u,v,w,w);
}
S = ;
T = n;
dijkstra();
build();
int y = m - bfs(),x = sap();
printf("%d %d\n",x,y);
}
return ;
}

SPFA貌似更快些,这图稀疏

 #include <bits/stdc++.h>
using namespace std;
using PII = pair<int,int>;
const int maxn = ;
const int INF = 0x3f3f3f3f;
struct arc {
int to,w,next;
arc(int x = ,int y = ,int z = -) {
to = x;
w = y;
next = z;
}
} e[];
int head[maxn],hd[maxn],tot,S,T,n,m;
int gap[maxn],d[maxn];
bool in[maxn] = {};
void add(int head[maxn],int u,int v,int wa,int wb) {
e[tot] = arc(v,wa,head[u]);
head[u] = tot++;
e[tot] = arc(u,wb,head[v]);
head[v] = tot++;
}
void dijkstra() {
memset(d,0x3f,sizeof d);
queue<int>q;
d[S] = ;
q.push(S);
while(!q.empty()){
int u = q.front();
q.pop();
in[u] = false;
for(int i = hd[u]; ~i; i = e[i].next){
if(d[e[i].to] > d[u] + e[i].w){
d[e[i].to] = d[u] + e[i].w;
if(!in[e[i].to]){
in[e[i].to] = true;
q.push(e[i].to);
}
}
}
}
}
void build() {
for(int i = ; i <= n; ++i) {
for(int j = hd[i]; ~j; j = e[j].next) {
if(d[e[j].to] == d[i] + e[j].w)
add(head,i,e[j].to,,);
}
}
}
int bfs() {
memset(gap,,sizeof gap);
memset(d,-,sizeof d);
queue<int>q;
d[T] = ;
q.push(T);
while(!q.empty()) {
int u = q.front();
q.pop();
++gap[d[u]];
for(int i = head[u]; ~i; i = e[i].next) {
if(d[e[i].to] == -) {
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[S];
}
int dfs(int u,int low) {
if(u == T) return low;
int tmp = ,minH = n - ;
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].w) {
if(d[e[i].to] + == d[u]) {
int a = dfs(e[i].to,min(e[i].w,low));
e[i].w -= a;
e[i^].w += a;
tmp += a;
low -= a;
if(!low) break;
if(d[S] >= n) return tmp;
}
if(e[i].w) minH = min(minH,d[e[i].to]);
}
}
if(!tmp) {
if(--gap[d[u]] == ) d[S] = n;
++gap[d[u] = minH + ];
}
return tmp;
}
int sap(int ret = ) {
while(d[S] < n) ret += dfs(S,INF);
return ret;
}
int main() {
int u,v,w;
while(~scanf("%d%d",&n,&m)) {
memset(head,-,sizeof head);
memset(hd,-,sizeof hd);
for(int i = tot = ; i < m; ++i) {
scanf("%d%d%d",&u,&v,&w);
add(hd,u,v,w,w);
}
S = ;
T = n;
dijkstra();
build();
int y = m - bfs(),x = sap();
printf("%d %d\n",x,y);
}
return ;
}

2015 Multi-University Training Contest 1 Tricks Device的更多相关文章

  1. 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 ...

  2. 2015 Multi-University Training Contest 1(7/12)

    2015 Multi-University Training Contest 1 A.OO's Sequence 计算每个数的贡献 找出第\(i\)个数左边最靠右的因子位置\(lp\)和右边最靠左的因 ...

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

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

  4. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

  5. 2015 UESTC Winter Training #8【The 2011 Rocky Mountain Regional Contest】

    2015 UESTC Winter Training #8 The 2011 Rocky Mountain Regional Contest Regionals 2011 >> North ...

  6. 2015 UESTC Winter Training #7【2010-2011 Petrozavodsk Winter Training Camp, Saratov State U Contest】

    2015 UESTC Winter Training #7 2010-2011 Petrozavodsk Winter Training Camp, Saratov State U Contest 据 ...

  7. Root(hdu5777+扩展欧几里得+原根)2015 Multi-University Training Contest 7

    Root Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Su ...

  8. 2015 Multi-University Training Contest 6 solutions BY ZJU(部分解题报告)

    官方解题报告:http://bestcoder.hdu.edu.cn/blog/2015-multi-university-training-contest-6-solutions-by-zju/ 表 ...

  9. HDU 5360 Hiking(优先队列)2015 Multi-University Training Contest 6

    Hiking Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total S ...

随机推荐

  1. sdoi2013 spring(hash+容斥)

    大体思路是先求出来\(f[i]\)代表有至少\(i\)个位置相同的点对数. 然后就已经没什么好害怕的了(跟BZOJ3622一样) 然后这个\(f[i\)]怎么求呢? 最无脑的方法就是枚举位置,然后\( ...

  2. JS侧边栏实现

    <!DOCTYPE html> <html lang="en"> <style> </style> <head> < ...

  3. Redis-server在windows下闪退

    在win7下使用Redis(windows版)很简单,只需要去Git上下载一个压缩包,解压运行即可.但是前段时间发现win10下双击redis-server既然闪退.非常不解... 在观察了错误日志才 ...

  4. navicat连接SQL Sever数据库出现的问题

    1.navicat 连接sqlserver提示要安装 sql server native client 解决办法:在navicat安装目录下有个sqlncli_x64.msi文件,双击安装一直下一步就 ...

  5. java源码之TreeSet

    1,TreeSet介绍 1)TreeSet 是一个有序的集合,它的作用是提供有序的Set集合.2)TreeSet 继承于AbstractSet,所以它是一个Set集合,具有Set的属性和方法.3)Tr ...

  6. [Angular + Unit Testing] Mock HTTP Requests made with Angular’s HttpClient in Unit Tests

    In a proper unit test we want to isolate external dependencies as much as possible to guarantee a re ...

  7. poj2492--A Bug&#39;s Life(并查集变形)

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28703   Accepted: 9350 De ...

  8. 2 怎样解析XML文件或字符串

    1 引用XML文件 2 使用XMLReader解析文本字符串 3 使用XMLReader方法读取XML数据 详细代码实现例如以下: //初始化一个XML字符串 String xmlString = @ ...

  9. Android View 事件分发机制 源代码解析 (上)

    一直想写事件分发机制的文章,无论咋样,也得自己研究下事件分发的源代码.写出心得~ 首先我们先写个简单的样例来測试View的事件转发的流程~ 1.案例 为了更好的研究View的事件转发,我们自定以一个M ...

  10. node--20 moogose demo2

    db.js /** * Created by Danny on 2015/9/28 16:44. */ //引包 var mongoose = require('mongoose'); //创建数据库 ...