【POJ1637】Sightseeing tour

本文探讨了如何通过调整图中边的方向来构建欧拉回路的问题,利用最大流算法解决了一个城市旅游路线的设计挑战。

 

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

 

 

题意:

  给出一张既有单向边又有双向边的图,给其中的双向边定向之后使得图构成欧拉回路,问是否存在可行方案。

 

分析:

对于无向边,我们不知道方向,就随便定向,然后再想想怎么修改。

定完向之后,求所有点的出度与入度,如果任意一个点出度和入度的差为奇数,那么图将无法构成欧拉回路。

(先假设,一个点的出度和入度分别为out和in,如果出度和入度的差|out-in|为奇数,这时有a条连向这个点的边改变了方向,b条从这个点向外连的边改变了方向,那么它的入度就变为了in-a+b,出度变为了out-b+a,出度和入度的差|out-b+a-in+a-b|=|out-in+2a-2b|还是奇数,所以无论如何将边重定向,图都无法构成欧拉回路。)

对于图中的一个点,如果in>out,则说明需要有(in - out)/2条出边需要改变方向,如果in<out,则说明需要有(out - in)/2条入边需要改变方向。而对于无向边(u,v),定义其方向为u->v,那么它将有可能被改为v->u。

剩下的问题用最大流就能解决,对于每个in>out的节点,从源向这个点连一条流量为(in-out)/2的边,对于每个in<out的点,从这个点向汇连一条流量为(out-in)/2的边。而对于每条被定向为u->v的无向边,从v向u连接一条流量为1的边,这条边被流过则说明(u,v)被改变了方向。

跑一遍最大流,如果最大的流量等于与汇相连的边的流量总和,则说明在将一些边重定向后可以满足所有点出度与入度相等,即可以构成欧拉回路。

 

代码:

  1 #include <cstdio>
  2 #include <cstring>
  3 
  4 const int maxn = 300;
  5 const int maxm = 3000;
  6 
  7 int et[maxm], ep[maxm], ef[maxm];
  8 int last[maxn], en;
  9 int p1, p2, dir;
 10 int t, n, m, sta, end, ans, ind[maxn];
 11 
 12 inline void insert(int from, int to, int flow)
 13 {
 14     en++;
 15     et[en] = to;
 16     ef[en] = flow;
 17     ep[en] = last[from];
 18     last[from] = en;
 19     en++;
 20     et[en] = from;
 21     ef[en] = 0;
 22     ep[en] = last[to];
 23     last[to] = en;
 24 }
 25 
 26 inline int min(int a, int b)
 27 {
 28     return a < b ? a : b;
 29 }
 30 
 31 int gap[maxn], dis[maxn], cur[maxn];
 32 
 33 int sap(int now, int flow)
 34 {
 35     if (now == end)
 36     {
 37         return flow;
 38     }
 39     int ret = 0, tmp;
 40     for (int e = cur[now]; e; cur[now] = e = ep[e])
 41     {
 42         if (et[e] > 0 && dis[now] == dis[et[e]] + 1)
 43         {
 44             tmp = sap(et[e], min(flow - ret, ef[e]));
 45             ret += tmp;
 46             ef[e] -= tmp;
 47             ef[e ^ 1] += tmp;
 48             if (ret == flow)
 49             {
 50                 return ret;
 51             }
 52         }
 53     }
 54     cur[now] = last[now];
 55     if (--gap[dis[now]] <= 0)
 56     {
 57         dis[sta] = end + 1;
 58     }
 59     ++gap[++dis[now]];
 60     return ret;
 61 }
 62 
 63 int maxflow()
 64 {
 65     memset(gap, 0, sizeof(gap));
 66     memset(dis, 0, sizeof(dis));
 67     int flow = 0;
 68     gap[0] = end;
 69     for (int i = 1; i <= end; i++)
 70     {
 71         cur[i] = last[i];
 72     }
 73     while (dis[sta] <= end)
 74     {
 75         flow += sap(sta, 2147483647);
 76     }
 77     return flow;
 78 }
 79 
 80 int main()
 81 {
 82     scanf("%d", &t);
 83     while (t--)
 84     {
 85         memset(last, 0, sizeof(last));
 86         memset(ind, 0, sizeof(ind));
 87         en = 1;
 88         ans = 0;
 89         scanf("%d%d", &n, &m);
 90         sta = n + 1;
 91         end = sta + 1;
 92         for (int i = 0; i < m; i++)
 93         {
 94             scanf("%d%d%d", &p1, &p2, &dir);
 95             ind[p1]--;
 96             ind[p2]++;
 97             if (!dir)
 98             {
 99                 insert(p2, p1, 1);
100             }
101         }
102         bool flag = 1;
103         for (int i = 1; i <= n; i++)
104         {
105             if (ind[i] % 2 > 0)
106             {
107                 flag = 0;
108                 break;
109             }
110             if (ind[i] > 0)
111             {
112                 insert(sta, i, ind[i] / 2);
113             }
114             if (ind[i] < 0)
115             {
116                 insert(i, end, -ind[i] / 2);
117                 ans += -ind[i] / 2;
118             }
119         }
120         if (flag == 0)
121         {
122             puts("impossible");
123         }
124         else
125         {
126             puts(maxflow() == ans ? "possible" : "impossible");
127         }
128     }
129 }

 

转载于:https://www.cnblogs.com/lightning34/p/4579263.html

AI-PPT 一键生成 PPT:用户输入主题关键词,AI-PPT 可快速生成完整 PPT,涵盖标题、正文、段落结构等,还支持对话式生成,用户可在 AI 交互窗口边查看边修改。 文档导入转 PPT:支持导入 Word、Excel、PDF 等多种格式文档,自动解析文档结构,将其转换为结构清晰、排版规范的 PPT,有保持原文和智能优化两种模式。 AI-PPT 对话 实时问答:用户上传 PPT 或 PPTX 文件后,可针对演示内容进行提问,AI 实时提供解答,帮助用户快速理解内容。 多角度内容分析:对 PPT 内容进行多角度分析,提供全面视野,帮助用户更好地把握内容结构和重点。 多语言对话支持:支持多语言对话,打破语言障碍,方便不同语言背景的用户使用。 AI - 绘图 文生图:用户输入文字描述,即可生成符合语义的不同风格图像,如油画、水彩、中国画等,支持中英文双语输入。 图生图:用户上传图片并输入描述,AI - 绘图能够根据参考图和描述生成新的风格化图像,适用于需要特定风格或元素的创作需求。 图像编辑:提供如 AI 超清、AI 扩图、AI 无痕消除等功能,用户可以上传图片进行细节修改和优化,提升图片质量。 AI - 文稿 文案生成:能够根据用户需求生成多种类型的文章,如市场营销文案、技术文档、内部沟通内容等,提升文案质量和创作效率。 文章润色:对已有文章进行改善和优化,包括语言表达、逻辑连贯性、内容流畅度等方面,使文章更符合用户期望和风格。 文章续写:AI 技术理解文本语境,为用户提供新的想法、补充资料或更深层次的见解,帮助用户丰富文档内容。 AI - 医生 智能健康咨询:包括症状自查,用户输入不适症状,AI 结合病史等信息提供疾病可能性分析与初步建议;用药指导,支持查询药品适应症、禁忌症等,并预警潜在冲突;中医辨证,提供体质辨识与调理建议。 医学报告解读:用户上传体检报告
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值