设为首页 收藏本站
查看: 1128|回复: 0

[经验分享] POJ 1637 Sightseeing tour (SAP | Dinic 混合欧拉图的判断)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-9-20 08:42:40 | 显示全部楼层 |阅读模式


Sightseeing tour


Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6448 Accepted: 2654

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
  
  给出一张混合图(有有向边,也有无向边),判断是否存在欧拉回路。
  首先是对图中的无向边随意定一个方向,然后统计每个点的入度(indeg)和出度(outdeg),如果(indeg - outdeg)是奇数的话,一定不存在欧拉回路;
  如果所有点的入度和出度之差都是偶数,那么就开始网络流构图:
  1,对于有向边,舍弃;对于无向边,就按照最开始指定的方向建权值为 1 的边;
  2,对于入度小于出度的点,从源点连一条到它的边,权值为(outdeg - indeg)/2;出度小于入度的点,连一条它到汇点的权值为(indeg - outdeg)/2 的边;
  构图完成,如果满流(求出的最大流值 == 和汇点所有连边的权值之和),那么存在欧拉回路,否则不存在。
  另附一个讲解欧拉图不错的博客:http://www.cnblogs.com/destinydesigner/archive/2009/09/28/1575674.html
  SAP果然快,0ms:



#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int VM=210;
const int EM=1010;
const int INF=0x3f3f3f3f;
struct Edge{
int to,nxt;
int cap;
}edge[EM<<1];
int n,m,cnt,head[VM];
int src,des,tot,sum,indeg[VM],outdeg[VM];
int dep[VM],gap[VM],cur[VM],aug[VM],pre[VM];
void addedge(int cu,int cv,int cw){
edge[cnt].to=cv;    edge[cnt].cap=cw;   edge[cnt].nxt=head[cu];
head[cu]=cnt++;
edge[cnt].to=cu;    edge[cnt].cap=0;   edge[cnt].nxt=head[cv];
head[cv]=cnt++;
}
int SAP(int n){
int max_flow=0,u=src,v;
int id,mindep;
aug[src]=INF;
pre[src]=-1;
memset(dep,0,sizeof(dep));
memset(gap,0,sizeof(gap));
gap[0]=n;
for(int i=0;i<=n;i++)
cur=head; // 初始化当前弧为第一条弧
while(dep[src]<n){
int flag=0;
if(u==des){
max_flow+=aug[des];
for(v=pre[des];v!=-1;v=pre[v]){     // 路径回溯更新残留网络
id=cur[v];
edge[id].cap-=aug[des];
edge[id^1].cap+=aug[des];
aug[v]-=aug[des];   // 修改可增广量,以后会用到
if(edge[id].cap==0) // 不回退到源点,仅回退到容量为0的弧的弧尾
u=v;
}
}
for(int i=cur;i!=-1;i=edge.nxt){
v=edge.to;    // 从当前弧开始查找允许弧
if(edge.cap>0 && dep==dep[v]+1){  // 找到允许弧
flag=1;
pre[v]=u;
cur=i;
aug[v]=min(aug,edge.cap);
u=v;
break;
}
}
if(!flag){
if(--gap[dep]==0)    /* gap优化,层次树出现断层则结束算法 */
break;
mindep=n;
cur=head;
for(int i=head;i!=-1;i=edge.nxt){
v=edge.to;
if(edge.cap>0 && dep[v]<mindep){
mindep=dep[v];
cur=i;   // 修改标号的同时修改当前弧
                }
}
dep=mindep+1;
gap[dep]++;
if(u!=src)  // 回溯继续寻找允许弧
u=pre;
}
}
return max_flow;
}
void Init(){
cnt=0;
memset(head,-1,sizeof(head));
memset(indeg,0,sizeof(indeg));
memset(outdeg,0,sizeof(outdeg));
}
int main(){
//freopen("input.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--){
Init();
scanf("%d%d",&n,&m);
int u,v,c;
for(int i=0;i<m;i++){
scanf("%d%d%d",&u,&v,&c);
indeg[v]++;
outdeg++;
if(c==0)
addedge(u,v,1);
}
int flag=1;
for(int i=1;i<=n;i++)
if((indeg-outdeg)%2==1){
flag=0;
break;
}
if(!flag)
puts("impossible");
else{
sum=0;
src=0,  des=n+1;
for(int i=1;i<=n;i++){  //无向边建图,有向边舍弃
if(indeg<outdeg)
addedge(src,i,(outdeg-indeg)/2);
else if(indeg>outdeg){
addedge(i,des,(indeg-outdeg)/2);
sum+=(indeg-outdeg)/2;
}
}
int ans=SAP(des+1);
if(sum==ans)
puts("possible");
else
puts("impossible");
}
}
return 0;
}
  



#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int VM=210;
const int EM=1010;
const int INF=0x3f3f3f3f;
struct Edge{
int u,v,nxt;
int cap;
}edge[EM<<1];
int n,m,cnt,head[VM];
int src,des,tot,sum,dep[VM],indeg[VM],outdeg[VM];
void addedge(int cu,int cv,int cw){
edge[cnt].u=cu;     edge[cnt].v=cv;     edge[cnt].cap=cw;
edge[cnt].nxt=head[cu];     head[cu]=cnt++;
edge[cnt].u=cv;     edge[cnt].v=cu;     edge[cnt].cap=0;
edge[cnt].nxt=head[cv];     head[cv]=cnt++;
}
void Init(){
cnt=0;
memset(head,-1,sizeof(head));
memset(indeg,0,sizeof(indeg));
memset(outdeg,0,sizeof(outdeg));
}
int BFS(){
queue<int> q;
while(!q.empty())
q.pop();
memset(dep,-1,sizeof(dep));
dep[src]=0;
q.push(src);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head;i!=-1;i=edge.nxt){
int v=edge.v;
if(edge.cap>0 && dep[v]==-1){    //没有标记,且可行流大于0
dep[v]=dep+1;
q.push(v);
}
}
}
return dep[des]!=-1;    //汇点是否成功标号,也就是说是否找到增广路
}
int DFS(int u,int minx){
if(u==des)
return minx;
int tmp;
for(int i=head;i!=-1;i=edge.nxt){
int v=edge.v;
if(edge.cap>0 && dep[v]==dep+1 && (tmp=DFS(v,min(minx,edge.cap)))){
edge.cap-=tmp;
edge[i^1].cap+=tmp;
return tmp;
}
}
dep=-1;
return 0;
}
int Dinic(){
int ans=0,tmp;
while(BFS()){
while(1){
tmp=DFS(src,INF);
if(tmp==0)
break;
ans+=tmp;
}
}
return ans;
}
int main(){
//freopen("input.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--){
Init();
scanf("%d%d",&n,&m);
int u,v,c;
for(int i=0;i<m;i++){
scanf("%d%d%d",&u,&v,&c);
indeg[v]++;
outdeg++;
if(c==0)
addedge(u,v,1);
}
int flag=1;
for(int i=1;i<=n;i++)
if((indeg-outdeg)%2==1){
flag=0;
break;
}
if(!flag)
puts("impossible");
else{
sum=0;
src=0,  des=n+1;
for(int i=1;i<=n;i++){  //无向边建图,有向边舍弃
if(indeg<outdeg)
addedge(src,i,(outdeg-indeg)/2);
else if(indeg>outdeg){
addedge(i,des,(indeg-outdeg)/2);
sum+=(indeg-outdeg)/2;
}
}
int ans=Dinic();
if(sum==ans)
puts("possible");
else
puts("impossible");
}
}
return 0;
}
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-116045-1-1.html 上篇帖子: SAP物料管理(MM)名词解释 下篇帖子: SAP:建表时如果有QUAN、CURR类型的字段不能激活的问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表