2015 Multi-University Training Contest 1 Tricks Device
Tricks Device
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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.
#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的更多相关文章
- 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 ...
- 2015 Multi-University Training Contest 1(7/12)
2015 Multi-University Training Contest 1 A.OO's Sequence 计算每个数的贡献 找出第\(i\)个数左边最靠右的因子位置\(lp\)和右边最靠左的因 ...
- HDU 5294 Tricks Device(多校2015 最大流+最短路啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294 Problem Description Innocent Wu follows Dumb Zha ...
- 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 ...
- 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 ...
- 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 据 ...
- 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 ...
- 2015 Multi-University Training Contest 6 solutions BY ZJU(部分解题报告)
官方解题报告:http://bestcoder.hdu.edu.cn/blog/2015-multi-university-training-contest-6-solutions-by-zju/ 表 ...
- 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 ...
随机推荐
- sdoi2013 spring(hash+容斥)
大体思路是先求出来\(f[i]\)代表有至少\(i\)个位置相同的点对数. 然后就已经没什么好害怕的了(跟BZOJ3622一样) 然后这个\(f[i\)]怎么求呢? 最无脑的方法就是枚举位置,然后\( ...
- JS侧边栏实现
<!DOCTYPE html> <html lang="en"> <style> </style> <head> < ...
- Redis-server在windows下闪退
在win7下使用Redis(windows版)很简单,只需要去Git上下载一个压缩包,解压运行即可.但是前段时间发现win10下双击redis-server既然闪退.非常不解... 在观察了错误日志才 ...
- navicat连接SQL Sever数据库出现的问题
1.navicat 连接sqlserver提示要安装 sql server native client 解决办法:在navicat安装目录下有个sqlncli_x64.msi文件,双击安装一直下一步就 ...
- java源码之TreeSet
1,TreeSet介绍 1)TreeSet 是一个有序的集合,它的作用是提供有序的Set集合.2)TreeSet 继承于AbstractSet,所以它是一个Set集合,具有Set的属性和方法.3)Tr ...
- [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 ...
- poj2492--A Bug's Life(并查集变形)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28703 Accepted: 9350 De ...
- 2 怎样解析XML文件或字符串
1 引用XML文件 2 使用XMLReader解析文本字符串 3 使用XMLReader方法读取XML数据 详细代码实现例如以下: //初始化一个XML字符串 String xmlString = @ ...
- Android View 事件分发机制 源代码解析 (上)
一直想写事件分发机制的文章,无论咋样,也得自己研究下事件分发的源代码.写出心得~ 首先我们先写个简单的样例来測试View的事件转发的流程~ 1.案例 为了更好的研究View的事件转发,我们自定以一个M ...
- node--20 moogose demo2
db.js /** * Created by Danny on 2015/9/28 16:44. */ //引包 var mongoose = require('mongoose'); //创建数据库 ...