POJ 637 Sightseeing tour 混合欧拉回路 最大流

Sightseeing tour
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9114 Accepted: 3841

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


题意:给一个既有有向边又有无向边的图,问这个图是否存在欧拉回路。

       欧拉回路就是从一个点出发,经过每条边一次并回到起点的路径。这个只问是否存在,不用找路径。对于有向图,要求每个点的入度==出度就有欧拉回路,关于欧拉回路:点击打开链接

       做法:要使有欧拉回路,就要使每个点的入度==出度,方法是网络流,首先是将无向边任意定向,计算每个点的入度和出度,如果有某个点的出入度之差为奇数,就肯定不存在欧拉回路,为偶数可以改变与这个点连接的无向边的方向来改变出入度(有向边反向点的出度+1,入度-1或者出度-1,入度+1)。这是一个判断条件。

       然后就是关于网络流建模,建立源点s,汇点t。有向边方向已定,不能改变点的出入度,计算了关于有向边的点的出入度后,删掉。我们只关心无向边的方向,所以只在图中保存随意定向的无向边,流量为1。对于出度>入度的点i,它需要入度,建边(s,i,(out[i]-in[i])/2),出度<入度,需要出度,建边(i,t,(in[i]-out[i])/2)。然后跑一次最大流,如果能满流就是存在欧拉回路的,否则不存在。

       关于正确性,每一次可行流必然是由s出发,经过一个出>入的点,最后经过一个入>出的点到t,将这个可行流上的边都反向,是不是对于出>入的点出度-1,入度+1,入>出的点出度+1,入度-1,这里不考虑s和t连的边对点的出入度影响,因为这只是为了求解最大流新建的边,不是原图中的边,对原图中的点的出入度没有影响。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
const int N = 300+10;
const int INF = 1e8;
struct node{
    int v,flow,next;
    node(){}
    node(int v,int flow,int next):
        v(v),flow(flow),next(next){}
}E[N*100];

int n,m,top,s,t;
int head[N];  ///邻接表头结点
int in[N];    ///入度
int out[N];   ///出度
int d[N];     ///dinic的dis
bool vis[N];  ///dinic用

void Init()
{
    top = 0;
    for(int i = 0;i < N;i++){
        head[i] = -1;
        in[i] = out[i] = 0;
    }
}

void add(int u,int v,int flow)
{
    E[top] = node(v,flow,head[u]);
    head[u] = top++;
    E[top] = node(u,0,head[v]);
    head[v] = top++;
}

bool bfs()
{
    memset(vis,false,sizeof vis);
    memset(d,0,sizeof d);
    queue<int>q;
    vis[s] = true;
    q.push(s);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int i = head[u];i != -1;i = E[i].next){
            int v = E[i].v;
            if(vis[v] || E[i].flow == 0) continue;
            d[v] = d[u]+1;
            vis[v] = true;
            q.push(v);
        }
    }
    return vis[t];
}

int dfs(int u,int a)
{
    if(u == t || a == 0){
        return a;
    }
    int flow = 0;
    for(int i = head[u];i != -1;i = E[i].next){
        int v = E[i].v;
        if(d[v] != d[u]+1 || E[i].flow == 0) continue;
        int f = dfs(v,min(a,E[i].flow));
        if(f > 0){
            a -= f;
            E[i].flow -= f;
            E[i^1].flow += f;
            flow += f;
        }
    }
    return flow;
}

int dinic()  ///小红书最大流模板
{
    int flow = 0;
    while(bfs()){
        flow += dfs(s,INF);
    }
    return flow;
}

int main(void)
{
    int T;
    scanf("%d",&T);
    while(T--){
        Init();
        s = 210,t = 211;
        scanf("%d%d",&n,&m);
        for(int i = 1;i <= m;i++){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            out[u]++;
            in[v]++;
            if(w == 0){     ///只建无向向边的图,定向u->v
                add(u,v,1);
            }
        }
        bool flag = false;
        for(int i = 1;i <= n;i++){
            if(abs(in[i]-out[i]%2 == 1))  ///出入度差为奇数
                flag = true;
            else{
                if(in[i] > out[i])
                    add(i,t,(in[i]-out[i])/2);
                if((in[i] < out[i]))
                    add(s,i,(out[i]-in[i])/2);
            }
        }
        if(flag){
            printf("impossible\n");
            continue;
        }
        int tot = 0;  ///满流总量
        for(int i = head[s];i != -1;i = E[i].next){
            tot += E[i].flow;
        }
        int ans = dinic();
        if(ans == tot)
            printf("possible\n");
        else
            printf("impossible\n");
    }
    return 0;
}


智慧旅游解决方案利用云计算、物联网和移动互联网技术,通过便携终端设备,实现对旅游资源、经济、活动和旅游者信息的智能感知和发布。这种技术的应用旨在提升游客在旅游各个环节的体验,使他们能够轻松获取信息、规划行程、预订票务和安排食宿。智慧旅游平台为旅游管理部门、企业和游客提供服务,包括政策发布、行政管理、景区安全、游客流量统计分析、投诉反馈等。此外,平台还提供广告促销、库存信息、景点介绍、电子门票、社交互动等功能。 智慧旅游的建设规划得到了国家政策的支持,如《国家中长期科技发展规划纲要》和国务院的《关于加快发展旅游业的意见》,这些政策强调了旅游信息服务平台的建设和信息化服务的重要性。随着技术的成熟和政策环境的优化,智慧旅游的时机已经到来。 智慧旅游平台采用SaaS、PaaS和IaaS等云服务模式,提供简化的软件开发、测试和部署环境,实现资源的按需配置和快速部署。这些服务模式支持旅游企业、消费者和管理部门开发高性能、高可扩展的应用服务。平台还整合了旅游信息资源,提供了丰富的旅游产品创意平台和统一的旅游综合信息库。 智慧旅游融合应用面向游客和景区景点主管机构,提供无线城市门户、智能导游、智能门票及优惠券、景区综合安防、车辆及停车场管理等服务。这些应用通过物联网和云计算技术,实现了旅游服务的智能化、个性化和协同化,提高了旅游服务的自由度和信息共享的动态性。 智慧旅游的发展标志着旅游信息化建设的智能化和应用多样化趋势,多种技术和应用交叉渗透至旅游行业的各个方面,预示着全面的智慧旅游时代已经到来。智慧旅游不仅提升了游客的旅游体验,也为旅游管理和服务提供了高效的技术支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值