POJ 3684 Priest John's Busiest Day 2-SAT+输出路径
强连通算法推断是否满足2-sat,然后反向建图,拓扑排序+染色。
一种选择是从 起点開始,还有一种是终点-持续时间那个点 開始。
若2个婚礼的某2种时间线段相交,则有矛盾,建边。
easy出错的地方就在于推断线段相交。
若s1<e2&&s2<e1则相交
输出路径的做法能够參考论文2-SAT解法浅析
#include <iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
#define MAXN 5555
#define MAXM 3000010
struct node
{
int to,next;
}edge[MAXM];
int head[MAXN],en;
int low[MAXN],dfn[MAXN],stack[MAXN],cho[MAXN],top,set[MAXN],col,num,color[MAXN],counts[MAXN];
bool vis[MAXN],instack[MAXN];
int n;
int m;
void addedge(int a,int b)
{
edge[en].to=b;
edge[en].next=head[a];
head[a]=en++;
}
int son[MAXN];
void tarjan(int u)
{ vis[u]=1;
dfn[u]=low[u]=++num;
instack[u]=true;
stack[++top]=u;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(!vis[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else
if(instack[v])
low[u]=min(dfn[v],low[u]);
}
if(dfn[u]==low[u])
{
int j;
col++;
do
{
j=stack[top--];
instack[j]=false;
set[j]=col;
}
while (j!=u);
}
}
void init()
{
en=top=col=num=0;
memset(head,-1,sizeof(head));
memset(instack,0,sizeof(instack));
memset(vis,0,sizeof(vis));
memset(set,-1,sizeof(set));
memset(color,0,sizeof(color));
memset(counts,0,sizeof(counts));
memset(cho,0,sizeof(cho));
}
struct node2
{
int st,ed,la;
}p[2005];
vector<int> g[2005];
void topsort()
{
queue<int> q;
for(int i=1;i<=col;i++)
{
if(counts[i]==0)
q.push(i);
}
while(!q.empty())
{
int j=q.front();
if(!color[j]) color[j]=1,color[son[j]]=-1;
q.pop();
for(int k=0;k<g[j].size();k++)
{
if(--counts[g[j][k]]==0)
q.push(g[j][k]);
}
}
}
void print(int t1,int t2)
{ printf("%02d:%02d",t1/60,t1%60);
printf(" %02d:%02d\n",t2/60,t2%60);
}
int main()
{
int a,b,c,d,e;
while(~scanf("%d",&n))
{
init();
for(int i=1;i<=n;i++)
{
scanf("%d:%d%d:%d%d",&a,&b,&c,&d,&e);
p[i].st=a*60+b;
p[i].ed=c*60+d;
p[i].la=e;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
if(p[i].st<p[j].st+p[j].la && p[i].st+p[i].la>p[j].st) {addedge(i,j+n);}
if(p[i].st<p[j].ed && p[i].st+p[i].la>p[j].ed-p[j].la) {addedge(i,j);} if(p[i].ed-p[i].la<p[j].st+p[j].la && p[i].ed>p[j].st) {addedge(i+n,j+n);}
if(p[i].ed-p[i].la<p[j].ed && p[i].ed>p[j].ed-p[j].la) {addedge(i+n,j);}
}
}
for(int i=1;i<=n*2;i++)
if(!vis[i])tarjan(i);
int ok=1;
for(int i=1;i<=n;i++)
{
if(set[i]==set[i+n]) {ok=0;break;}
son[set[i]]=set[i+n];
son[set[i+n]]=set[i];
}
if(ok==0) {puts("NO");continue;}
else puts("YES");
for(int i=1;i<=col;i++) g[i].clear();
for(int i=1;i<=2*n;i++)
for(int j=head[i];~j;j=edge[j].next)
if(set[i]!=set[edge[j].to])
{
g[set[edge[j].to]].push_back(set[i]);
counts[set[i]]++;
}
topsort(); for(int i=1;i<=2*n;i++)
{
if(color[set[i]]==1) cho[i]=1;
}
for(int i=1;i<=n;i++)
{
if(cho[i]) print(p[i].st,p[i].st+p[i].la);
else print(p[i].ed-p[i].la,p[i].ed);
}
}
return 0;
}
/*
3
08:00 09:00 30
08:15 09:00 20
08:00 09:00 30
*/
POJ 3684 Priest John's Busiest Day 2-SAT+输出路径的更多相关文章
- POJ 3683 Priest John's Busiest Day (2-SAT+输出可行解)
题目地址:POJ 3683 第一次做须要输出可行解的题目. . .大体思路是先用强连通来推断是否有可行解,然后用逆序建图.用拓扑排序来进行染色.然后输出可行解. 详细思路见传送门 由于推断的时候少写了 ...
- HDU2491 Priest John's Busiest Day
题目链接 题意: 有n个人要进行乒乓球比赛,每一个人都一个能力值.每一个人出现的次序就是他们住的位置 如今要求进行一场比赛,三个人,裁判的能力值在两个选手之间,住的位置也在两个人的之间 问这样的比赛一 ...
- POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题)
POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题) Descripti ...
- poj 3686 Priest John's Busiest Day
http://poj.org/problem?id=3683 2-sat 问题判定,输出一组可行解 http://www.cnblogs.com/TheRoadToTheGold/p/8436948. ...
- POJ 3683 Priest John's Busiest Day (2-SAT)
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6900 Accept ...
- POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10010 Accep ...
- POJ 3683 Priest John's Busiest Day(2-SAT 并输出解)
Description John is the only priest in his town. September 1st is the John's busiest day in a year b ...
- poj - 3683 - Priest John's Busiest Day(2-SAT)
题意:有N场婚礼,每场婚礼的开始时间为Si,结束时间为Ti,每场婚礼有个仪式,历时Di,这个仪式要么在Si时刻开始,要么在Ti-Di时刻开始,问能否安排每场婚礼举行仪式的时间,使主持人John能参加所 ...
- POJ 3683 Priest John's Busiest Day (2-SAT)
题意:有n对新人要在同一天结婚.结婚时间为Ti到Di,这里有时长为Si的一个仪式需要神父出席.神父可以在Ti-(Ti+Si)这段时间出席也可以在(Di-Si)-Si这段时间.问神父能否出席所有仪式,如 ...
随机推荐
- hdu 4963(中途相遇法)
题目链接:Dividing a String 题意:给定一个2*n(n<=20)的字符串及每个位置的字符包含的权重,求将该字符串分成两个子序列S1.T1,要求S1=T1且abs(weight1- ...
- openGl超级宝典学习笔记 (1)第一个三角形
执行效果 代码及解析: // // Triangle.cpp // Triangle // // Created by fengsser on 15/6/20. // Copyright (c) 20 ...
- 模拟spring框架注入实现原理
这个我是参见了别人的一些东西,不是原创! 定义一些抽象的方法: package com.huxin.springinject.dao; public interface Person { public ...
- 读取USB HDD(USB移动硬盘信息)序列号的代码
读取USB HDD(USB移动硬盘)序列号的代码,型号及分位. 使用Visual Studio 2010编译成功. 代码使用了CrystalDiskInfo中的代码smartata.c中相关代码: 例 ...
- UVA 11100 The Trip, 2007 贪心(输出比较奇葩)
题意:给出n个包的大小,规定一个大包能装一个小包,问最少能装成几个包. 只要排序,然后取连续出现次数最多的数的那个次数.输出注意需要等距输出. 代码: /* * Author: illuz <i ...
- 【mysql】关于子查询的一个例子
假设表my_tbl包含三个字段a,b,c:现在需要查询表中列a的每个不同值下的列b为最小值的记录量. 比如表记录为: a b c 1 3 'cd' 2 3 'nhd' 1 5 'bg' ...
- SE 2014年4月18日
实验需求: R1 R2 R3用环回口建立IBGP对等体(使用对等体组),AS号为100 R4 R5 R6用环回口建立IBGP对等体(使用对等体组),AS号为 ...
- java单例模式(线程安全,效率高,双重推断)
这样的方法,在获取单利的时候,避免了线程锁,导致訪问该方法速度非常慢, 同是,防止了多线程同事房屋该方法就会产生多个实例的问题. 效率高.线程安全. public class TestInstance ...
- php(LAMP)开发环境配置相关问题及解决办法
相信很多像我一样初次接触到php开发的人,在配置基本的开发环境时都是一头雾水,为此小编特写下自己在安装配置php开发环境过程中遇到的一些问题,及解决办法. 1.LAMP组合,安装centons+apa ...
- GIT分支管理是一门艺术(转)
英文原文:http://www.nvie.com/posts/a-successful-git-branching-model/ 原文作者:Vincent Driessen 本文经Linux大棚博主总 ...