hdu 3599 War(最短路+最大流)
来源:互联网 发布:进销存软件排行 编辑:程序博客网 时间:2024/05/16 17:50
求一个无向图中边不重复的最短路条数。
建图:
1、先利用spfa或者dijkstra求一次最短路,记录起点到每个点的最短距离。
2、根据起点到每个点的最短距离d[i]重新建图,如果某条边的权值为w,两个顶点为u,v,若low[u]+w=low[v],即满足u到v最短,那么u到v连一条有向边,其边权为1。
3、求出新图中的最大流,即为所求的最短路条数。
超级大坑点:n=1特判,不然TLE到死。
#include<iostream>#include<cstdio>#include<cmath>#include<map>#include<set>#include<algorithm>#include<queue>#include<stack>#include<cstdlib>#include<cstring>#include<vector>#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;typedef long long LL;typedef unsigned long long uLL;typedef __int64 LI;typedef unsigned __int64 uLI;typedef unsigned int uI;typedef double db;#define maxn 2005#define inf 0x3f3f3f3fstruct e{ int to,cap,next;}e[3000005],e0[3000005];int cnt,head[maxn],d[maxn],s,t,n;inline void add(int u,int v,int w){ e[cnt].to=v; e[cnt].cap=w; e[cnt].next=head[u]; head[u]=cnt++; e[cnt].to=u; e[cnt].cap=0; e[cnt].next=head[v]; head[v]=cnt++;}inline bool bfs() //BFS构造层次图{ int u,i; queue<int> q; q.push(s); memset(d,0,sizeof(d)); d[s]=1; while(!q.empty()) { u=q.front();q.pop(); if(u==t) return true; //汇点的层次被算出即可停止,因为根据DFS的规则,和汇点同层或更下一层的结点是不可能走到汇点的。 for(i=head[u];~i;i=e[i].next){ int v=e[i].to; if(!d[v]&&e[i].cap) { d[v]=d[u]+1; q.push(v); } } } return false;}int dfs(int u,int a) //当前结点、目前为止所有弧的最小残量{ int flow=0,f,i; if(u==t||a==0) return a; for(i=head[u];~i;i=e[i].next) { int v=e[i].to; if(e[i].cap&&d[v]==d[u]+1) //满足容量不为0且另一端是下一层次 { f=dfs(v,min(a,e[i].cap)); e[i].cap-=f; //削减路径上各边的容量 e[i^1].cap+=f;//添加反向边 flow+=f; //增加总流量 a-=f; //该结点之前的弧的最小残量削减f后的值 if(!a) break; //若为0,则无需再从该结点寻找增广路,因为位于该结点之前的某条弧削减f后会为0,导致无法从源点经过该结点再增广到汇点 } } if(flow==0) d[u]=0; //从该点出发找不到增广路,则将该点从层次图中去掉 return flow;}bool vis[maxn];int low[maxn],cnt0,head0[maxn];inline void add0(int u,int v,int w){ e0[cnt0].to=v; e0[cnt0].cap=w; e0[cnt0].next=head0[u]; head0[u]=cnt0++;}void spfa(){ int i; memset(vis,0,sizeof(vis)); memset(low,inf,sizeof(low)); low[1]=0; vis[1]=1; queue<int> q; q.push(1); while(!q.empty()) { int u=q.front();q.pop(); vis[u]=0; for(i=head0[u]; ~i; i=e0[i].next) { int v=e0[i].to; if(low[v]>low[u]+e0[i].cap) { low[v]=low[u]+e0[i].cap; if(!vis[v]) { vis[v]=1; q.push(v); } } } }}int dinic(){ int ans=0; for(int i=0;i<=n;++i) for(int j=head0[i];~j;j=e0[j].next) { int v=e0[j].to; if(e0[j].cap+low[i]==low[v]) add(i,v,1); } s=1,t=n; while(bfs()) ans+=dfs(s,inf); return ans;}int main(){ int a,b,c,T; scanf("%d",&T); while(T--){ scanf("%d",&n); memset(head,-1,sizeof(head)); memset(head0,-1,sizeof(head0)); cnt0=cnt=0; while(scanf("%d%d%d",&a,&b,&c)){ if(a==0&&b==0&&c==0) break; add0(a,b,c); add0(b,a,c); } spfa(); if(n==1||low[n]==inf) puts("0"); else printf("%d\n",dinic()); } return 0;}
0 0
- hdu 3599 War(最短路+最大流)
- HDU 3599 War 求最短路+最大流
- hdu 3599(最短路+最大流)
- HDU 3599 最短路+最大流
- hdu 3416 最短路+最大流
- HDU 3416 最短路+最大流
- hdu 3416 最短路+最大流
- hdu 3416(最短路+最大流)
- 最短路+最大流 hdu 3416
- hdu 3416(最短路+最大流)
- HDU 3416 最短路+最大流
- hdu 5294 最短路+最大流
- hdu 3416(最短路+最大流)
- hdu 3416 Marriage Match IV 【网络最大流+最短路】
- HDU 3416 Marriage Match IV(最短路+最大流)
- Tricks Device (hdu 5294 最短路+最大流)
- hdu 5294 Tricks Device(最短路+最大流)
- hdu 5294 Tricks Device(最短路 + 最大流)
- Asteroids
- reverse integer
- LightOJ 1110 An Easy LCS
- 机房收费流程图分析(一)
- 使用virtualenv, uwsgi, nginx来布署flask
- hdu 3599 War(最短路+最大流)
- json解析 本地文件和网上文件(通过接口)
- python_学习笔记0817
- C#接口详解
- MD5算法
- socket之广播
- poj-2503 Babelfish
- 给中国学生的第二封信:从优秀到卓越
- centos下vi的用法大全