博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj1637
阅读量:6167 次
发布时间:2019-06-21

本文共 4074 字,大约阅读时间需要 13 分钟。

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
#include
#include
#include
#include
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"); }}

 

转载于:https://www.cnblogs.com/applejxt/p/4113907.html

你可能感兴趣的文章
nslookup错误
查看>>
我的友情链接
查看>>
Supported plattforms
查看>>
做自己喜欢的事情
查看>>
CRM安装(二)
查看>>
在JAVA中使用Thumbnails为图片加水印
查看>>
2013最常用的NoSQL数据库
查看>>
无聊写点东西
查看>>
shell自动发邮件通过QQ邮箱
查看>>
Linux入门及基础知识
查看>>
搭建基于nginx环境的nagios监控系统
查看>>
神舟电脑6月或登创业板 减少PC保修期增利润
查看>>
Linux NAT优化的校验和问题
查看>>
自动化部署必备技能—搭建YUM仓库
查看>>
高可用集群之heartbeat安装配置
查看>>
C++的面向对象特性
查看>>
SpringBoot整合ActiveMQ
查看>>
企业Shell面试题18:单词及字母去重排序案例
查看>>
64位linux安装lamp
查看>>
我的欧拉工程之路_13
查看>>