您的位置:首页 > 其它

POJ 1637 Sightseeing tour (混合图的欧拉回路)

2013-02-27 14:44 369 查看
Sightseeing tour

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6167 Accepted: 2540
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
4
5 8
2 1 0
1 3 0
4 1 1
1 5 0
5 4 1
3 4 0
4 2 1
2 2 0
4 4
1 2 1
2 3 0
3 4 0
1 4 1
3 3
1 2 0
2 3 0
3 2 0
3 4
1 2 0
2 3 1
1 2 0
3 2 0

Sample Output
possible
impossible
impossible
possible

Source

Northwestern Europe 2003

思路http://blog.csdn.net/hqd_acm/article/details/5860509

网上搜的题解,顺便学习下关于欧拉回路的知识,说实在的不是很懂....尤其是建图那块.....

#include<cstdio>
using namespace std;
const int mm=1000000;
const int mn=22222;
const int oo=1000000000;
int node,s,t,edge,n,m;
int indegree[mn],outdegree[mn];
int to[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn];
inline int Abs(int a)
{
return a<0?(-a):a;
}
inline int min(int a,int b)
{
return a<b?a:b;
}
inline void init(int nn,int ss,int tt)
{
node=nn,s=ss,t=tt,edge=0;
for(int i=0; i<node; ++i) head[i]=-1;
}
inline void add(int u,int v,int c1,int c2=0)
{
to[edge]=v,flow[edge]=c1,next[edge]=head[u],head[u]=edge++;
to[edge]=u,flow[edge]=c2,next[edge]=head[v],head[v]=edge++;
}
bool bfs()
{
int i,u,v,l,r=0;
for(i=0; i<node; ++i) dis[i]=-1;
dis[q[r++]=s]=0;
for(l=0; l<r; ++l)
for(i=head[u=q[l]]; i>=0; i=next[i])
if(flow[i]&&dis[v=to[i]]<0)
{
dis[q[r++]=v]=dis[u]+1;
if(v==t)return 1;
}
return 0;
}
int dfs(int u,int maxf)
{
if(u==t) return maxf;
for(int &i=work[u],v,tmp; i>=0; i=next[i])
if(flow[i]&&dis[v=to[i]]==dis[u]+1&&(tmp=dfs(v,min(maxf,flow[i])))>0)
{
flow[i]-=tmp;
flow[i^1]+=tmp;
return tmp;
}
return 0;
}
int dinic()
{
int i,ret=0,delta;
while(bfs())
{
for(i=0; i<node; ++i) work[i]=head[i];
while(delta=dfs(s,oo))ret+=delta;
}
return ret;
}
int main()
{
int i,a,b,ca,op;
scanf("%d",&ca);
while(ca--)
{
scanf("%d%d",&n,&m);
init(n+2,0,n+1);
for(i=1; i<=n; ++i)
indegree[i]=outdegree[i]=0;
for(i=0; i<m; i++)
{
scanf("%d%d%d",&a,&b,&op);
++indegree[b],++outdegree[a];
if(!op)
add(a,b,1);
}
for(i=1; i<=n; i++)
if(Abs(indegree[i]-outdegree[i])%2)
break;
if(i<=n)
{
printf("impossible\n");
continue;
}
int sum=0;
for(i=1; i<=n; ++i)
{
if(indegree[i]>outdegree[i])
add(i,t,(indegree[i]-outdegree[i])/2);
else
{
add(s,i,(outdegree[i]-indegree[i])/2);
sum+=(outdegree[i]-indegree[i])/2;
}
}
if(sum==dinic())
printf("possible\n");
else printf("impossible\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: