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

本文介绍了一种通过算法解决城市观光巴士线路规划的问题,确保每条街道被访问一次且最终回到起点,探讨了如何判断这样的线路是否可行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

思路http://blog.youkuaiyun.com/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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值