2015 ACM / ICPC 北京站 热身赛 C题

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std; const int INF=0x7FFFFFFF;
const int maxn=+;//点的数量
int n,m,k;
int u[+],v[+];
int dis1[maxn],dis2[maxn];
bool flag1[maxn],flag2[maxn];
vector<int>P[maxn];
vector<int>FP[maxn];
queue<int>Q1;
queue<int>Q2; 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>edges;
vector<int>G[maxn*];
bool vis[maxn*];
int d[maxn*];
int cur[maxn*];
int s, t; void read()
{
for(int i=;i<=m;i++)
{
scanf("%d%d",&u[i],&v[i]);
P[u[i]].push_back(v[i]);
FP[v[i]].push_back(u[i]);
}
} void init()
{
for(int i=;i<maxn;i++) P[i].clear();
for(int i=;i<maxn;i++) FP[i].clear(); for(int i=;i<=n;i++) dis1[i]=INF,dis2[i]=INF; for(int i = ; i < *maxn; i++) G[i].clear();
edges.clear();
} void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge(from, to, cap, ));
edges.push_back(Edge(to, from, , ));
int w = edges.size();
G[from].push_back(w - );
G[to].push_back(w - );
} bool BFS()
{
memset(vis, , sizeof(vis));
queue<int>Q;
Q.push(s);
d[s] = ;
vis[s] = ;
while (!Q.empty())
{
int x = Q.front();
Q.pop();
for (int i = ; i<G[x].size(); i++)
{
Edge e = edges[G[x][i]];
if (!vis[e.to] && e.cap>e.flow)
{
vis[e.to] = ;
d[e.to] = d[x] + ;
Q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x, int a)
{
if (x == t || a == )
return a;
int flow = , f;
for (int &i = cur[x]; i<G[x].size(); i++)
{
Edge e = edges[G[x][i]];
if (d[x]+ == d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>)
{
edges[G[x][i]].flow+=f;
edges[G[x][i] ^ ].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
if(!flow) d[x] = -;
return flow;
} int dinic(int s, int t)
{
int flow = ;
while (BFS())
{
memset(cur, , sizeof(cur));
flow += DFS(s, INF);
}
return flow;
} void spfa1(int x)
{
while(!Q1.empty()) Q1.pop();
memset(flag1,,sizeof flag1); dis1[x]=;
flag1[x]=;
Q1.push(x); while(!Q1.empty())
{
int h=Q1.front(); flag1[h]=; Q1.pop();
for(int i=;i<P[h].size();i++)
{
if(dis1[h]+<dis1[P[h][i]])
{
dis1[P[h][i]]=dis1[h]+;
if(flag1[P[h][i]]==)
{
flag1[P[h][i]]=;
Q1.push(P[h][i]);
}
}
}
}
} void spfa2(int x)
{
while(!Q2.empty()) Q2.pop();
memset(flag2,,sizeof flag2); dis2[x]=;
flag2[x]=;
Q2.push(x); while(!Q2.empty())
{
int h=Q2.front(); flag2[h]=; Q2.pop();
for(int i=;i<FP[h].size();i++)
{
if(dis2[h]+<dis2[FP[h][i]])
{
dis2[FP[h][i]]=dis2[h]+;
if(flag2[FP[h][i]]==)
{
flag2[FP[h][i]]=;
Q2.push(FP[h][i]);
}
}
}
}
} int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
if(!n&&!m&&!k) break; init();
read(); spfa1();
spfa2(n); s=;t=n+n; for(int i=;i<=n;i++) AddEdge(i+n,i,); for(int i=;i<=m;i++)
if(dis1[u[i]]+dis2[v[i]]+<=k)
AddEdge(u[i],v[i]+n,INF); int ans=dinic(s,t);
printf("%d\n",ans);
}
return ;
}

HDU 2485 Destroying the bus stations的更多相关文章

  1. HDU 2485 Destroying the bus stations(!最大流∩!费用流∩搜索)

    Description Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “im ...

  2. 图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)

    Problem Description Gabiluso is one of the greatest spies in his country. Now he's trying to complet ...

  3. HDU 2485 Destroying the bus stations (IDA*+ BFS)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意:给你n个点,m条相连的边,问你最少去掉几个点使从1到n最小路径>=k,其中不能去掉1, ...

  4. HDU 2485 Destroying the bus stations(费用流)

    http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意: 现在要从起点1到终点n,途中有多个车站,每经过一个车站为1时间,现在要在k时间内到达终点,问至少要 ...

  5. hdu 2485 Destroying the bus stations 最小费用最大流

    题意: 最少需要几个点才能使得有向图中1->n的距离大于k. 分析: 删除某一点的以后,与它相连的所有边都不存在了,相当于点的容量为1.但是在网络流中我们只能直接限制边的容量.所以需要拆点来完成 ...

  6. HDUOJ----2485 Destroying the bus stations(2008北京现场赛A题)

    Destroying the bus stations                                                                          ...

  7. Destroying the bus stations

    Destroying the bus stations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1832   Acce ...

  8. Destroying the bus stations HDU - 2485(最小割点)

    题意: 就是求最小割点 解析: 正向一遍spfa 反向一遍spfa  然后遍历每一条边,对于当前边 如果dis1[u] + dis2[v] + 1 <= k 那么就把这条边加入到网络流图中, 每 ...

  9. POJ 3921 Destroying the bus stations 沿着最短路迭代加深搜索

    题目:给出一个图,问最少删除多少个点,使得从点1到点n经过的点数超过k个. 分析: 上网搜了一下,发现很多人用网络流做的,发现我不会.再后来看到这篇说网络流的做法是错的,囧. 后来发现点数有点少,直接 ...

随机推荐

  1. nefu 519 昨日重现

    昨日重现 Problem : 519 Time Limit : 1000ms Memory Limit : 65536K description 兴安黑熊在高中学习数学时,曾经知道这样一个公式:f(n ...

  2. python2.6.6在centos6.4下安装

    1.wget http://www.python.org/ftp/python/2.6.6/Python-2.6.6.tar.bz2 2. tar xvjf Python-2.6.6.tar.bz2 ...

  3. sublime Text 常用操作

    原文出处:http://www.php100.com/html/it/focus/2014/1030/7666.html 1. 多光标操作:只要按下Cmd(Windows系统下Ctrl)键,再用鼠标选 ...

  4. 获得正在编辑行的数据 esayui datagrid

    function getEditRow(datagridId) {//datagridId为table容器的id var input = $('#' + datagridId).parent().fi ...

  5. 循环ip段 转载 出处不明

    public struct IP         {             public byte A;             public byte B;             public  ...

  6. Android Studio一直build、一直refreshing、一直buiding gradle project into的终极解决办法

    打开我的电脑,打开C:\Users\用户名, 把红圈的文件夹都删了 如果AndroidStudioProjects文件夹里那些project都不重要,也可以跟红圈文件夹删了, 然后再打开android ...

  7. MySQL-测试卷一

    MySQL-测试卷一 一.单项选择题 1 下面不属于Msql数据库特点的是(  ) A. 免费使用  B.不能跨平台  C.开源软件  D.功能强大 2 定义表的一个字段, 要求能表示4位整数,2位小 ...

  8. flex超链接

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  9. 【转】哦,mysql 的其它发行版本Percona, mariadb

    原文:http://geek.csdn.net/news/detail/130146 2016年11月25日,沃趣科技"智慧应用 数据先行"2016产品发布会暨新三板挂牌庆祝会在杭 ...

  10. git简单常用的命令

    git status --查看文件状态 git add+文件路径 --上传到缓存区 git add --all --全部传到缓存区 git commit -m '描述' --对上传文件做描述 git ...