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. lldpd-0.7.7代码解读(send_pdu部分)

    此文档是经过逆序推到出的,可能有错误之处,敬请指教,谢谢. 1)interfaces_update 更新一些接口信息 2)levent_iface_subscribe 该接口通过socket通信(非阻 ...

  2. docker Swarm 集群发现

    Node 发现 $swarm manage --discovery dockerhost01:,dockerhost02:,dockerhost03: -H= 文件发现 $swarm manage - ...

  3. mysql中AES_ENCRYPT、AES_DNCRYPT及CONVERT的用法

    这两天在弄公司的服务端项目的时候,发现mysql比较实用的3个函数,在这里给大家分享一下. 1.AES_ENCRYPT函数,这个函数的使用场景是对一些安全性要求比较高的数据使用AES加密,语法为: A ...

  4. 使用PLSQL Developer连接Oracle Database 11g Express Edition

    要使用oracle数据库,需要准备三部分: 1.oracle服务端 2.oracle客户端 3.连接工具 你装的Oracle Database 11g Express Edition就是服务端,pls ...

  5. HttpRequest中常见的四种ContentType【转载】

    本文转自:http://www.aikaiyuan.com/6324.html HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.T ...

  6. Eclipse如何自定义format代码

  7. python http请求

    from httplib2 import Http def postDada(): http=Http() heads={"pragma":"no-cache" ...

  8. Inno Setup入门(十一)——完成安装后执行某些程序

    有些时候我们的程序虽然能够很好的完成安装,但是程序的配置工作可能需要其他的一些程序来辅助完成,如果不执行这些程序,主程序就不能很好的完成工作,甚至不能完成工作,一个很明显的例子是,目前许多程序是通过N ...

  9. iptables-1.4.19 移植到linux

    ------------------------------------------------------------------------------------------ https://g ...

  10. namenode ha

    http://blog.csdn.net/totxian/article/details/45248399 http://www.aboutyun.com/thread-13679-1-1.html ...