POJ - 1637 Sightseeing tour(混合图欧拉回路的求解--建图跑最大流)

题目链接:https://vjudge.net/contest/399194#problem/B

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

翻译:
能否建造一个旅游系统:城市的每条街道都能准确地参观一次,开始和结束在同一个路口。
输入
t:几组样例
m和s(1 <= m <= 200,1 <= s <= 1000 )
路口数和街道数

s条道路: xi, yi di(0 <= di <= 1)
di=1:xi—>yi
di=0:双向道路(无向边)
输出:“possible” or “impossible”,是否能建造一个观光旅游

分析:
把该图的无向边随便定向(从x–>y或从y–>x),计算每个点的入度和出度。
如果有某个点出入度之差为奇数,那么肯定不存在欧拉回路。

现在每个点入度和出度之差均为偶数。那么将这个偶数除以 2,得 x。
也就是说,对于每一个点,只要将 x 条边改变方向(入>出就是变入,出>入就是变出),就能保证出=入。
我该改变哪些边,可以让每个点出=入?
1. 有向边是不能改变方向的,要之无用,删
2. 无向边已经定向,边的容量为1

新建 s 和t。对于入>出的点 u,连接边(u, t)、容量为 x,对于出>入的点 v,连接边(s, u),容量为x。
察看是否有满流的分配。有就是能有欧拉回路,没有就是没有

邻接矩阵+EK

#include<cstdio>
#include<cstring>
#include<math.h>
#include<queue>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int N=2*1e2+10;
const int M=1e3+10;
const int INF=0x3f3f3f3f;
int m,s;
int ind[N];///记录入度,出度
int capacity[N][N],flow[N];
int pre[N];
bool book[N];
int bfs(int source,int sink)
{
    memset(pre,-1,sizeof(pre));
    memset(book,false,sizeof(book));
    queue<int>Q;
    Q.push(source);
    book[source]=true;
    pre[source]=0;
    flow[source]=INF;
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        if(u==sink)
            break;
        for(int i=0; i<=m+1; i++)
        {
            if(!book[i]&&capacity[u][i]>0)
            {
                pre[i]=u;
                book[i]=true;
                flow[i]=min(flow[u],capacity[u][i]);
                Q.push(i);
            }
        }
    }
    if(pre[sink]!=-1)
        return flow[sink];
    else
        return -1;
}
int max_flow(int source,int sink)
{
    memset(flow,0,sizeof(flow));
    int ans=0;
    for(;;)
    {
        int k=bfs(source,sink);
        if(k==-1)
            return ans;
        ans+=k;
        for(int i=sink; i!=source; i=pre[i])
        {
            capacity[pre[i]][i]-=k;
            capacity[i][pre[i]]+=k;
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(ind,0,sizeof(ind));
        memset(capacity,0,sizeof(capacity));
        scanf("%d%d",&m,&s);///m个结点 s条边
        while(s--)
        {
            int x,y,d;
            scanf("%d%d%d",&x,&y,&d);
            ind[x]--,ind[y]++;///出度--,入度++
            if(!d)///无向边
                capacity[x][y]+=1;
        }
        bool ck=true;
        for(int i=1; i<=m; i++)
        {
            if(ind[i]&1)///欧拉图
            {
                ck=false;
                break;
            }
        }
        if(!ck)
            printf("impossible\n");
        else
        {
            int sum=0;
            for(int i=1; i<=m; i++)
            {
                if(ind[i]<0)///>
                    capacity[0][i]=(-ind[i])>>1;
                if(ind[i]>0)//>
                {
                    capacity[i][m+1]=ind[i]>>1;
                    sum+=(ind[i]>>1);
                }
            }
            int mxx_flow=max_flow(0,m+1);
            if(mxx_flow==sum)
                printf("possible\n");
            else
                printf("impossible\n");
        }
    }
    return 0;
}

邻接表+dinic

#include<cstdio>
#include<cstring>
#include<math.h>
#include<queue>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int N=2*1e2+10;
const int M=2*1e5+10;
const int INF=0x3f3f3f3f;
int m,s;
struct node
{
    int nex;
    int to,w;
} side[M];
int fir[N],tot;///邻接表建图
int ind[N];///记录入度,出度
int level[N];///记录每一个点的层次
int cut[M];
void Inx(int x,int y,int z)
{
    side[++tot].to=y,side[tot].w=z;
    side[tot].nex=fir[x];
    fir[x]=tot;
}

int bfs()
{
    memset(level,-1,sizeof(level));
    queue<int>Q;
    level[0]=1;
    Q.push(0);
    while(!Q.empty())
    {
        int u=Q.front();
        if(u==m+1)
            break;
        Q.pop();
        for(int i=fir[u]; ~i; i=side[i].nex)
        {
            if(side[i].w&&level[side[i].to]<0)
            {
                level[side[i].to]=level[u]+1;///构建层次网络
                Q.push(side[i].to);
            }
        }
    }
    return level[m+1]!=-1;
}
int dfs(int u,int low)
{
    if(u==m+1)
        return low;
    int temp=0,a;
    for(int &i=cut[u]; ~i; i=side[i].nex)///i的改变带着fir[u]的改变,提高效率,所以用cut[]数组保留原来的fir[u]
    {
        if(side[i].w&&level[side[i].to]==level[u]+1&&(a=dfs(side[i].to,min(low,side[i].w))))
        {
            temp+=a;
            side[i].w-=a;
            side[i^1].w+=a;
            low-=a;
            if(!low)
                break;
        }
    }
    if(temp==0)
        level[u]=-1;///走到u不能走
    return temp;
}
int dinic()
{
    int ans=0;
    while(bfs())///找到一条增广路
    {
        for(int i=0; i<=m+1; i++)
            cut[i]=fir[i];
        ans+=dfs(0,INF);
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        tot=-1;
        memset(ind,0,sizeof(ind));
        memset(fir,-1,sizeof(fir));
        scanf("%d%d",&m,&s);
        while(s--)
        {
            int x,y,d;
            scanf("%d%d%d",&x,&y,&d);
            if(x==y)
                continue;
            ind[x]--,ind[y]++;///出度--,入度++
            if(!d)///无向边
            {
                Inx(x,y,1);///把无向边随便规定一个方向
                Inx(y,x,0);
            }
        }
        bool bo=true;
        for(int i=1; i<=m; i++)
        {
            if(ind[i]&1)///欧拉图
            {
                bo=false;
                break;
            }
        }
        if(!bo)
            printf("impossible\n");
        else
        {
            int sum=0;
            for(int i=1; i<=m; i++)
            {
                if(ind[i]<0)///>
                {
                    Inx(0,i,(-ind[i])>>1);
                    Inx(i,0,0);
                }
                if(ind[i]>0)//>
                {
                    Inx(i,m+1,ind[i]>>1);
                    Inx(m+1,i,0);
                    sum+=(ind[i]>>1);
                }
            }
            int mxx_flow=dinic();
            if(mxx_flow==sum)
                printf("possible\n");
            else
                printf("impossible\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zaiyang遇见

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值