【混合图欧拉路判定】【poj 1637】Sightseeing tour
2014-12-16 10:09
459 查看
http://poj.org/problem?id=1637
题解:http://www.cnblogs.com/kuangbin/p/3537525.html
题解:http://www.cnblogs.com/kuangbin/p/3537525.html
//#define _TEST _TEST #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <cmath> #include <algorithm> using namespace std; /************************************************ Code By willinglive Blog:http://willinglive.cf ************************************************/ #define rep(i,l,r) for(int i=(l),___t=(r);i<=___t;i++) #define per(i,r,l) for(int i=(r),___t=(l);i>=___t;i--) #define MS(arr,x) memset(arr,x,sizeof(arr)) #define LL long long #define INE(i,u,e) for(int i=head[u];~i;i=e[i].next) inline const int read() {int r=0,k=1;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1; for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;} ///////////////////////////////////////////////// const int inf=0x3f3f3f3f; int n,m; struct edge{int v,flow,cap,next;}e[2222]; int head[222],k; int ind[222],outd[222]; int S,T; int cur[222],d[222],q[222]; ///////////////////////////////////////////////// void adde(int u,int v,int f){e[k].v=v;e[k].flow=0;e[k].cap=f;e[k].next=head[u];head[u]=k++;} void ins(int u,int v,int f1,int f2){adde(u,v,f1);adde(v,u,f2);} inline int F(int i){return e[i].cap-e[i].flow;} bool bfs() { MS(d,-1); d[q[0]=S]=0; memcpy(cur,head,sizeof(cur)); for(int u,v,l=0,r=1;l<r;) { u=q[l++]; INE(i,u,e) if(F(i)&&d[v=e[i].v]==-1) { q[r++]=v; d[v]=d[u]+1; if(v==T) return 1; } } return 0; } int dfs(int u,int a) { if(u==T||a==0) return a; for(int &i=cur[u];~i;i=e[i].next) if(F(i)&&d[e[i].v]==d[u]+1) { if(int t=dfs(e[i].v,min(a,F(i)))) { e[i].flow+=t; e[i^1].flow-=t; return t; } } return 0; } int dinic() { int f=0; while(bfs()) while(int t=dfs(S,inf)) f+=t; return f; } ///////////////////////////////////////////////// void input() { MS(head,-1); k=0; MS(ind,0); MS(outd,0); scanf("%d%d",&n,&m); int u,v,w; rep(i,1,m) { scanf("%d%d%d",&u,&v,&w); ind[v]++; outd[u]++; if(w==0) ins(u,v,1,0); } } void solve() { S=0; T=n+1; rep(i,1,n) { if((ind[i]-outd[i])&1){puts("impossible");return;} } int sum=0; rep(i,1,n) { if(ind[i]<outd[i]) { ins(S,i,(outd[i]-ind[i])>>1,0); } else if(ind[i]>outd[i]) { ins(i,T,(ind[i]-outd[i])>>1,0); sum+=(ind[i]-outd[i])>>1; } } if(sum==dinic()) puts("possible"); else puts("impossible"); } ///////////////////////////////////////////////// int main() { #ifndef _TEST freopen("std.in","r",stdin); freopen("std.out","w",stdout); #endif rep(i,1,read()) input(),solve(); return 0; }
相关文章推荐
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
- POJ 1637 Sightseeing tour (混合图欧拉路判定)(可以转化为最大流)
- poj 1637 Sightseeing tour(混合图欧拉路径判定 最大流)
- POJ 1637 Sightseeing tour (欧拉路判定 网络流)
- Sightseeing tour POJ - 1637(混合欧拉回路判定)
- POJ1637 Sightseeing tour(混合图欧拉回路判定)
- POJ-1637 Sightseeing tour(通过网络流判定混合图的欧拉回路)
- Poj 1637 Sightseeing tour (混合图的欧拉回路判定)
- POJ 1637 Sightseeing tour混合欧拉图的判定
- POJ 1637 Sightseeing tour(混合欧拉回路,网络流)
- POJ 1637:Sightseeing tour 混合图欧拉回路
- POJ 1637 Sightseeing tour(混合图的欧拉回路)
- POJ 1637 Sightseeing tour (混合图欧拉回路,网络最大流)
- poj1637 Sightseeing tour,混合图的欧拉回路问题,最大流解
- POJ 1637 Sightseeing tour (网络流解决混合图欧拉回路问题)
- poj 1637 Sightseeing tour(混合欧拉回路)
- poj--1637--Sightseeing tour(网络流,最大流判断混合图是否存在欧拉图)
- POJ - 1637 Sightseeing tour (混合图欧拉路径欧拉图判断)
- poj 1637 Sightseeing tour 混合欧拉 最大流
- poj--1637--Sightseeing tour(网络流,最大流判断混合图是否存在欧拉图)