2015 多校赛 第一场 1007 (hdu 5294)
总算今天静下心来学算法。。
Description
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
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
Sample Input
Sample Output
for(int i=;i<=n;i++){
for(int j=;j<g[i].size();j++){
int tmp=d[i]-d[g[i][j]];
if(tmp<) tmp=-tmp;
if(tmp==e[i][j])
addEdge(i,g[i][j],e[i][j]);
}
}
举个例子:i 结点在最短路中前于 j 结点,e[i][j] 表示从 j 到 i 到有向边权值,则 d[i]-d[j]<0,则判了 d[j]-d[i]==e[i][j],错误。这在某些情况下回使得新建的图多了一条边。
for(int i=;i<=n;i++){
for(int j=;j<g[i].size();j++){
if(d[i]+e[i][j]==d[j])
addEdge(i,g[i][j],e[i][j]);
}
}
最小割的定义和求法:
定义:设点集S,T,称将起点在 S 中,终点在 T 中 的所有边删除后将使得 s 无法到达 t 的的集合划分(S,T)称为一个 s-t 割。(s 在 S 中 t 在 T 中)
在求得最大流后,寻找增广路过程中标记的点为集合 S,其余点为 T ,即得最小割。
最小割中的边数即为第一问的答案。(详情见代码)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define maxn 2015
#define inf 0x3f3f3f3f
struct Edge{
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
vector<Edge>E;
vector<int>g[maxn],e[maxn],G[maxn];
int n,m,M,a,b,c,d[maxn],vis[maxn],num[maxn],bet[maxn],p[maxn];
void init(){
for(int i=;i<=n;i++)
g[i].clear(),e[i].clear(),G[i].clear();
E.clear();
memset(vis,,sizeof(vis));
}
void addEdge(int from,int to,int cap){
E.push_back(Edge(from,to,cap,));
E.push_back(Edge(to,from,,));
M=E.size();
G[from].push_back(M-);
G[to].push_back(M-);
}
void maxflow(int s,int t){
for(;;){
memset(bet,,sizeof(bet));
queue<int>q;
q.push(s);
bet[s]=inf;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=;i<G[x].size();i++){
Edge &ee=E[G[x][i]];
if(!bet[ee.to]&&ee.cap>ee.flow){
p[ee.to]=G[x][i];
bet[ee.to]=min(bet[x],ee.cap-ee.flow);
q.push(ee.to);
}
}
if(bet[t]) break;
}
if(!bet[t]) break;
for(int u=t;u!=s;u=E[p[u]].from){
E[p[u]].flow+=bet[t];
E[p[u]^].flow-=bet[t];
}
}
}
void Dijk(){
memset(d,0x3f,sizeof(d));
memset(num,0x3f,sizeof(num));
d[]=num[]=;
for(int i=;i<=n;i++){
int u=-;
for(int j=;j<=n;j++) if(!vis[j]){
if(u==-||d[j]<d[u]) u=j;
}
vis[u]=;
for(int j=;j<g[u].size();j++) if(!vis[g[u][j]]){
int tmp=d[u]+e[u][j];
if(tmp==d[g[u][j]]) num[g[u][j]]=min(num[u]+,num[g[u][j]]);
if(tmp<d[g[u][j]]){
d[g[u][j]]=tmp;
num[g[u][j]]=num[u]+;
}
}
}
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
//freopen("test.txt","r",stdin);
while(~scanf("%d%d",&n,&m)){
init();
for(int i=;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
g[a].push_back(b),e[a].push_back(c);
g[b].push_back(a),e[b].push_back(c);
}
Dijk();
int ans1=,ans2=m-num[n];
for(int i=;i<=n;i++){
for(int j=;j<g[i].size();j++){
if(e[i][j]+d[i]==d[g[i][j]])
addEdge(i,g[i][j],e[i][j]);
}
}
maxflow(,n);
for(int i=;i<E.size();i++){
Edge &ee=E[i];
if(bet[ee.from]&&!bet[ee.to]&&ee.cap>) ans1++;
}
printf("%d %d\n",ans1,ans2);
}
return ;
}
以及Dinic:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define maxn 2015
#define inf 0x3f3f3f3f
struct Edge{
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
vector<Edge>E;
vector<int>g[maxn],e[maxn],G[maxn];
int n,m,M,a,b,c,d[maxn],vis[maxn],num[maxn],cur[maxn],dis[maxn];
void init(){
for(int i=;i<=n;i++)
g[i].clear(),e[i].clear(),G[i].clear();
E.clear();
memset(vis,,sizeof(vis));
}
void addEdge(int from,int to,int cap){
E.push_back(Edge(from,to,cap,));
E.push_back(Edge(to,from,,));
M=E.size();
G[from].push_back(M-);
G[to].push_back(M-);
}
bool bfs(){
memset(vis,,sizeof(vis));
queue<int>q;
q.push();
dis[]=;
vis[]=;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=;i<G[x].size();i++){
Edge &ee=E[G[x][i]];
if(!vis[ee.to]&&ee.cap>ee.flow){
vis[ee.to]=;
dis[ee.to]=dis[x]+;
q.push(ee.to);
}
}
}
return vis[n];
}
int dfs(int x,int a){
if(x==n||a==) return a;
int flow=,f;
for(int &i=cur[x];i<G[x].size();i++){
Edge &ee=E[G[x][i]];
if(dis[x]+==dis[ee.to]&& (f=dfs(ee.to,min(a,ee.cap-ee.flow)))>){
ee.flow+=f;
E[G[x][i]^].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
return flow;
}
void maxflow(){
while(bfs()){
memset(cur,,sizeof(cur));
dfs(,inf);
}
}
void Dijk(){
memset(d,0x3f,sizeof(d));
memset(num,0x3f,sizeof(num));
d[]=num[]=;
for(int i=;i<=n;i++){
int u=-;
for(int j=;j<=n;j++) if(!vis[j]){
if(u==-||d[j]<d[u]) u=j;
}
vis[u]=;
for(int j=;j<g[u].size();j++) if(!vis[g[u][j]]){
int tmp=d[u]+e[u][j];
if(tmp==d[g[u][j]]) num[g[u][j]]=min(num[u]+,num[g[u][j]]);
if(tmp<d[g[u][j]]){
d[g[u][j]]=tmp;
num[g[u][j]]=num[u]+;
}
}
}
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
//freopen("test.txt","r",stdin);
while(~scanf("%d%d",&n,&m)){
init();
for(int i=;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
g[a].push_back(b),e[a].push_back(c);
g[b].push_back(a),e[b].push_back(c);
}
Dijk();
int ans1=,ans2=m-num[n];
for(int i=;i<=n;i++){
for(int j=;j<g[i].size();j++){
if(d[i]+e[i][j]==d[g[i][j]])
addEdge(i,g[i][j],e[i][j]);
}
}
maxflow();
for(int i=;i<E.size();i++){
Edge &ee=E[i];
if(ee.from==ee.to) continue;
if(vis[ee.from]&&!vis[ee.to]&&ee.cap>) ans1++;
}
printf("%d %d\n",ans1,ans2);
}
return ;
}
2015 多校赛 第一场 1007 (hdu 5294)的更多相关文章
- 2015 多校赛 第一场 1002 (hdu 5289)
Description Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n ...
- 2015 多校赛 第一场 1001 (hdu 5288)
Description OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l&l ...
- hdu5294||2015多校联合第一场1007 最短路+最大流
http://acm.hdu.edu.cn/showproblem.php? pid=5294 Problem Description Innocent Wu follows Dumb Zhang i ...
- 2015 多校赛 第二场 1006 (hdu 5305)
Problem Description There are n people and m pairs of friends. For every pair of friends, they can c ...
- 2015 多校赛 第二场 1004 hdu(5303)
Problem Description There are n apple trees planted along a cyclic road, which is L metres long. You ...
- 2015 多校赛 第二场 1002 (hdu 5301)
Description Your current task is to make a ground plan for a residential building located in HZXJHS. ...
- hdu 5288||2015多校联合第一场1001题
pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...
- 2019牛客多校赛第一场 补题 I题
I题 Points Division 题意: 给你n个点,每个点有坐标(xi,yi)和属性(ai,bi),将点集划分为两个集合, 任意 A 集合的点 i 和 B 集合点 j, 不允许 xi > ...
- 2014多校第六场 1007 || HDU 4927 Series 1(杨辉三角组合数)
题目链接 题意 : n个数,每操作一次就变成n-1个数,最后变成一个数,输出这个数,操作是指后一个数减前一个数得到的数写下来. 思路 : 找出几个数,算得时候先不要算出来,用式子代替,例如: 1 2 ...
随机推荐
- 推荐几个好用的windows软件
好久没上了,= = 之前用了一段MAC,感觉好用的地方就是 spotlight和触摸板,哈哈 然后在用回windows之后就找了一下,我之前有用Launchy这个软件,但是他是用来启动程序的,当然你也 ...
- C++:C++在图片特定区域之外产生随机数
参考原文:C++产生随机数 (整数) C++在图片特定区域之外产生随机数,避开正则表达式,可以分为两种情况. 第一种:在某个数之前生成随机数:第二种,生成随机数,加上某个数,然后截断:第三种,指定范围 ...
- PAT_A1126#Eulerian Path
Source: PAT A1126 Eulerian Path (25 分) Description: In graph theory, an Eulerian path is a path in a ...
- NOIP2009 T2 Hankson的趣味题
传送门 题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上, ...
- [poj1325] Machine Schedule (二分图最小点覆盖)
传送门 Description As we all know, machine scheduling is a very classical problem in computer science a ...
- html绑定点击事件传值
1.方法1function radioSelect(event){ var valueRadio=event.value; if(valueRadio =='目的口岸'){ return '目的口岸' ...
- PHP学习总结(12)——PHP入门篇之变量
一.什么是变量 变量是用于存储值的,我们命令服务器去干活的时候,往往需要产生一些数据,需要临时性存放起来,方便取用.我们也可以理解为,变量就像一个购物袋,我们可以用来装苹果.榴莲(当然也可以用来装玫瑰 ...
- COGS——C1176. [郑州101中学] 月考
http://cogs.pro/cogs/problem/problem.php?pid=1176 [题目描述] 在上次的月考中Bugall同学违反了考场纪律还吃了处分,更可气的是在第二天的校会时 间 ...
- [Angular] Advanced DI
In this post, we are going to see how to solve one design pattern challenge. The challenge is what w ...
- 随机获取一个集合(List, Set)中的元素,随机获取一个Map中的key或value
利用Java提供的Random类.从List或Set中随机取出一个元素,从Map中随机获取一个key或value. 因为Set没有提供get(int index)方法,仅仅能先获取一个随机数后.利用一 ...