首页 > 代码库 > poj1637
poj1637
Sightseeing tour
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7796 | Accepted: 3264 |
Description
The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can see every corner of the beautiful city. They want to construct the tour so that every street in the city is visited exactly once. The bus should also start and end at the same junction. As in any city, the streets are either one-way or two-way, traffic rules that must be obeyed by the tour bus. Help the executive board and determine if it‘s possible to construct a sightseeing tour under these constraints.
Input
On the first line of the input is a single positive integer n, telling the number of test scenarios to follow. Each scenario begins with a line containing two positive integers m and s, 1 <= m <= 200,1 <= s <= 1000 being the number of junctions and streets, respectively. The following s lines contain the streets. Each street is described with three integers, xi, yi, and di, 1 <= xi,yi <= m, 0 <= di <= 1, where xi and yi are the junctions connected by a street. If di=1, then the street is a one-way street (going from xi to yi), otherwise it‘s a two-way street. You may assume that there exists a junction from where all other junctions can be reached.
Output
For each scenario, output one line containing the text "possible" or "impossible", whether or not it‘s possible to construct a sightseeing tour.
Sample Input
45 82 1 01 3 04 1 11 5 05 4 13 4 04 2 12 2 04 41 2 12 3 03 4 01 4 13 31 2 02 3 03 2 03 41 2 02 3 11 2 03 2 0
Sample Output
possibleimpossibleimpossiblepossible
netflow:首先要使这个图为欧拉回路,需满足:所有点的入度等于出度,这样就比较好建模了。
我们对于无向边按照输入的顺度定向,这样有可能造一些点不满足条件,我们发现对于一个点,如果它出入度之差为z,那么通过这个点的边至少需要修改z/2条,那么对于一个点,如果入度大于出变,建边(s,i,z/2),出度大于入度则建边(i,t,z/2),对于某条无向边,如果开始定义的方向为x->z,建边(z,x,1),(注意,只建(z,x,1),因为x->z贡献了z的入度,所以改这条边是减少z的入度,再加出我们对于入度大于点连s,所以这条边只能由z流向x。
#include<cstdio>#include<cstdlib>#include<algorithm>#include<cmath>#include<cstring>using namespace std;int dis[2011],g[2011],que[2011],d[2011];int next[23411],y[23411],flow[23411];int tt,data,tl,n,m,tot,x,z,kind,i,j,xzq,sum,s,t;bool pl;void star(int i,int j,int k){ tt++; next[tt]=g[i]; g[i]=tt; y[tt]=j; flow[tt]=k; tt++; next[tt]=g[j]; g[j]=tt; y[tt]=i; flow[tt]=0;}void Bfs(){ int l,r,x,j,k; memset(dis,255,sizeof(dis)); dis[s]=0; que[l=r=1]=s; while(l<=r){ x=que[l]; j=g[x]; while(j!=0){ k=y[j]; if(flow[j]>0&&dis[k]==-1){ r++; que[r]=k; dis[k]=dis[x]+1; } j=next[j]; } l++; }}int Dfs(int x,int fl){ if(x==t)return fl; int j,k,z,e; z=0; j=g[x]; while(j!=0){ k=y[j]; if(flow[j]>0&&dis[k]==dis[x]+1){ e=Dfs(k,min(flow[j],fl)); z+=e; fl-=e; flow[j]-=e; flow[j^1]+=e; if(!fl)return z; } j=next[j]; } dis[x]=-1; return z;}void Dinic(){ while(true){ Bfs(); if(dis[t]==-1)break; xzq+=Dfs(s,0x7fffffff); }}int main(){ scanf("%d",&data); for(tl=1;tl<=data;tl++){ memset(g,0,sizeof(g)); memset(d,0,sizeof(d)); tt=1; scanf("%d%d",&n,&m); for(i=1;i<=m;i++){ scanf("%d%d%d",&x,&z,&kind); if(x==z)continue; if(kind==1){ d[z]++; d[x]--; } else{ d[z]++; d[x]--; star(z,x,1); } } s=n+1; t=n+2; xzq=0; sum=0; pl=true; for(i=1;i<=n;i++){ if(abs(d[i])%2!=0)pl=false; if(d[i]>0){ star(s,i,d[i]/2); sum+=d[i]/2; } if(d[i]<0)star(i,t,-d[i]/2); } Dinic(); if(xzq!=sum||pl==false)printf("impossible\n"); else printf("possible\n"); }}
poj1637
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。