标签:destroying the bus s hdu 2485 网络流+最短路
5 7 3 1 3 3 4 4 5 1 2 2 5 1 4 4 5 0 0 0
2
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define MAXN 1005 #define MAXM 10005 #define mod 1000000009 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-6 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define FRE(i,a,b) for(i = a; i <= b; i++) #define FREE(i,a,b) for(i = a; i >= b; i--) #define FRL(i,a,b) for(i = a; i < b; i++) #define FRLL(i,a,b) for(i = a; i > b; i--) #define mem(t, v) memset ((t) , v, sizeof(t)) #define sf(n) scanf("%d", &n) #define sff(a,b) scanf("%d %d", &a, &b) #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) #define pf printf #define DBG pf("Hi\n") typedef long long ll; using namespace std; struct Edge { int to,next,cap,flow; }edge[MAXM]; int tol; int head[MAXN]; int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN]; void init() { tol=0; memset(head,-1,sizeof(head)); } //加边,单向图三个参数,双向图四个参数 void addedge(int u,int v,int w,int rw=0) { edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u]; edge[tol].flow=0; head[u]=tol++; edge[tol].to=u; edge[tol].cap=rw; edge[tol].next=head[v]; edge[tol].flow=0; head[v]=tol++; } //输入参数:起点,终点,点的总数 //点的编号没有影响,只要输入点的总数 int sap(int start,int end,int N) { memset(gap,0,sizeof(gap)); memset(dep,0,sizeof(dep)); memcpy(cur,head,sizeof(head)); int u=start; pre[u]=-1; gap[0]=N; int ans=0; while (dep[start]<N) { if (u==end) { int Min=INF; for (int i=pre[u];i!=-1;i=pre[edge[i^1].to]) if (Min>edge[i].cap-edge[i].flow) Min=edge[i].cap-edge[i].flow; for (int i=pre[u];i!=-1;i=pre[edge[i^1].to]) { edge[i].flow+=Min; edge[i^1].flow-=Min; } u=start; ans+=Min; continue; } bool flag=false; int v; for (int i=cur[u];i!=-1;i=edge[i].next) { v=edge[i].to; if (edge[i].cap-edge[i].flow && dep[v]+1==dep[u]) { flag=true; cur[u]=pre[v]=i; break; } } if (flag) { u=v; continue; } int Min=N; for (int i=head[u];i!=-1;i=edge[i].next) if (edge[i].cap-edge[i].flow && dep[edge[i].to]<Min) { Min=dep[edge[i].to]; cur[u]=i; } gap[dep[u]]--; if (!gap[dep[u]]) return ans; dep[u]=Min+1; gap[dep[u]]++; if (u!=start) u=edge[pre[u]^1].to; } return ans; } struct EDGE { int u,v,w,next; }E1[MAXM],E2[MAXM]; int n,m,k,num1,num2; int head1[MAXN],head2[MAXN],dist1[MAXN],dist2[MAXN],inq[MAXN]; void init2() { num1=0;num2=0; memset(head1,-1,sizeof(head1)); memset(head2,-1,sizeof(head2)); } void add1(int u,int v) { E1[num1].u=u; E1[num1].v=v; E1[num1].w=1; E1[num1].next=head1[u]++; head1[u]=num1++; } void add2(int u,int v) { E2[num2].u=u; E2[num2].v=v; E2[num2].w=1; E2[num2].next=head2[u]++; head2[u]=num2++; } void spfa(int s,int* head,int* dist,EDGE * E) { queue<int>Q; memset(inq,0,sizeof(inq)); dist[s]=0; inq[s]=1; Q.push(s); while (!Q.empty()) { int u=Q.front(); Q.pop(); for (int i=head[u];i+1;i=E[i].next) { int v=E[i].v; if (dist[v]>dist[u]+E[i].w) { dist[v]=dist[u]+E[i].w; if (!inq[v]) { inq[v]=1; Q.push(v); } } } } } int main() { int i,j,u,v; while (scanf("%d%d%d",&n,&m,&k)) { if (n==0&&m==0&&k==0) break; init(); init2(); for (i=0;i<m;i++) { scanf("%d%d",&u,&v); add1(u,v); add2(v,u); } memset(dist1,INF,sizeof(dist1)); memset(dist2,INF,sizeof(dist2)); spfa(1,head1,dist1,E1); spfa(n,head2,dist2,E2); for (i=0;i<num1;i++) { if (dist1[E1[i].u]+dist2[E1[i].v]<k) addedge(E1[i].u+n,E1[i].v,INF); } for (i=1;i<=n;i++) addedge(i,i+n,1); printf("%d\n",sap(n+1,n,2*n)); } return 0; }
Destroying the bus stations (hdu 2485 网络流+最短路)
标签:destroying the bus s hdu 2485 网络流+最短路
原文地址:http://blog.csdn.net/u014422052/article/details/45345979