http://poj.org/problem?id=3683

2-sat 问题判定,输出一组可行解

http://www.cnblogs.com/TheRoadToTheGold/p/8436948.html

注:

本代码在判断两个时间段部分有误,数据弱A了

  1. #include<cstdio>
  2. #include<vector>
  3.  
  4. using namespace std;
  5.  
  6. #define N 1001
  7.  
  8. struct TIME
  9. {
  10. int h1,m1;
  11. int h2,m2;
  12. int tim;
  13. }e[N];
  14.  
  15. int n;
  16. int tot;
  17.  
  18. int front[N<<],to[N*N*],nxt[N*N*];
  19.  
  20. int dfn[N<<],low[N<<];
  21. int st[N<<],top;
  22. bool vis[N<<];
  23.  
  24. int cnt;
  25. int id[N<<];
  26. vector<int>V[N<<];
  27.  
  28. int FRONT[N<<],TO[N*N*],NXT[N*N*],TOT;
  29. int in[N<<];
  30.  
  31. bool use[N<<],cut[N<<];
  32.  
  33. struct ANS
  34. {
  35. int h1,m1;
  36. int h2,m2;
  37. }ans[N];
  38.  
  39. void add(int u,int v)
  40. {
  41. to[++tot]=v; nxt[tot]=front[u]; front[u]=tot;
  42. }
  43.  
  44. void ADD(int u,int v)
  45. {
  46. TO[++TOT]=v; NXT[TOT]=FRONT[u]; FRONT[u]=TOT;
  47. in[v]++;
  48. }
  49.  
  50. bool judge(int h1s,int m1s,int h1t,int m1t,int h2s,int m2s,int h2t,int m2t)
  51. {
  52. int s1=(h1s-)*+m1s;
  53. int t1=(h1t-)*+m1t;
  54. int s2=(h2s-)*+m2s;
  55. int t2=(h2t-)*+m2t;
  56. if(s1<=s2)
  57. {
  58. if(s2>=t1) return false;
  59. return true;
  60. }
  61. else
  62. {
  63. if(t2<=s1) return false;
  64. return true;
  65. }
  66. }
  67.  
  68. void tarjan(int u)
  69. {
  70. dfn[u]=low[u]=++tot;
  71. st[++top]=u;
  72. vis[u]=true;
  73. for(int i=front[u];i;i=nxt[i])
  74. {
  75. if(!dfn[to[i]])
  76. {
  77. tarjan(to[i]);
  78. low[u]=min(low[u],low[to[i]]);
  79. }
  80. else if(vis[to[i]]) low[u]=min(low[u],dfn[to[i]]);
  81. }
  82. if(dfn[u]==low[u])
  83. {
  84. cnt++;
  85. while(st[top]!=u)
  86. {
  87. id[st[top]]=cnt;
  88. vis[st[top]]=false;
  89. V[cnt].push_back(st[top--]);
  90. }
  91. id[u]=cnt;
  92. vis[u]=false;
  93. V[cnt].push_back(u);
  94. top--;
  95. }
  96. }
  97.  
  98. void rebuild()
  99. {
  100. for(int k=;k<=n;++k)
  101. {
  102. int i=k<<;
  103. for(int j=front[i];j;j=nxt[j])
  104. if(id[i]!=id[to[j]]) ADD(id[to[j]],id[i]);
  105. i=k<<|;
  106. for(int j=front[i];j;j=nxt[j])
  107. if(id[i]!=id[to[j]]) ADD(id[to[j]],id[i]);
  108. }
  109. }
  110.  
  111. void out(int h,int m)
  112. {
  113. if(m> && m<) printf("%02d:%02d",h,m);
  114. else if(m<)
  115. {
  116. if(!(m%))
  117. {
  118. h+=m/;
  119. printf("%02d:00",h);
  120. }
  121. else
  122. {
  123. h+=m/;
  124. h--;
  125. m%=;
  126. if(m<) m+=;
  127. printf("%02d:%02d",h,m);
  128. }
  129. }
  130. else
  131. {
  132. h+=m/;
  133. m%=;
  134. printf("%02d:%02d",h,m);
  135. }
  136. }
  137.  
  138. void topsort()
  139. {
  140. for(int i=;i<=cnt;++i)
  141. if(!in[i]) st[++top]=i;
  142. int u,v;
  143. while(top)
  144. {
  145. u=st[top--];
  146. if(cut[u]) continue;
  147. use[u]=true;
  148. v=id[V[u][]^];
  149. cut[v]=true;
  150. for(int i=FRONT[u];i;i=NXT[i])
  151. {
  152. in[TO[i]]--;
  153. if(!in[TO[i]]) st[++top]=TO[i];
  154. }
  155. //for(int i=FRONT[v];i;i=NXT[i]) cut[TO[i]]=true;
  156. }
  157. for(int i=;i<=n;++i)
  158. if(use[id[i<<]])
  159. {
  160. ans[i].h1=e[i].h1;
  161. ans[i].m1=e[i].m1;
  162. ans[i].h2=e[i].h1;
  163. ans[i].m2=e[i].m1+e[i].tim;
  164. }
  165. else
  166. {
  167. ans[i].h1=e[i].h2;
  168. ans[i].m1=e[i].m2-e[i].tim;
  169. ans[i].h2=e[i].h2;
  170. ans[i].m2=e[i].m2;
  171. }
  172. puts("YES");
  173. for(int i=;i<=n;++i)
  174. {
  175. out(ans[i].h1,ans[i].m1);
  176. putchar(' ');
  177. out(ans[i].h2,ans[i].m2);
  178. putchar('\n');
  179. }
  180. }
  181.  
  182. int main()
  183. {
  184. scanf("%d",&n);
  185. for(int i=;i<=n;++i)
  186. {
  187. scanf("%d:%d",&e[i].h1,&e[i].m1);
  188. scanf("%d:%d",&e[i].h2,&e[i].m2);
  189. scanf("%d",&e[i].tim);
  190. }
  191. for(int i=;i<n;++i)
  192. for(int j=i+;j<=n;++j)
  193. if(i!=j)
  194. {
  195. if(judge(e[i].h1,e[i].m1,e[i].h1,e[i].m1+e[i].tim,e[j].h1,e[j].m1,e[j].h1,e[j].m1+e[j].tim))
  196. add(i<<,j<<|),add(j<<,i<<|);
  197. if(judge(e[i].h1,e[i].m1,e[i].h1,e[i].m1+e[i].tim,e[j].h2,e[j].m2-e[j].tim,e[j].h2,e[j].m2))
  198. add(i<<,j<<),add(j<<|,i<<|);
  199. if(judge(e[i].h2,e[i].m2-e[i].tim,e[i].h2,e[i].m2,e[j].h1,e[j].m1,e[j].h1,e[j].m1+e[j].tim))
  200. add(i<<|,j<<|),add(j<<,i<<);
  201. if(judge(e[i].h2,e[i].m2-e[i].tim,e[i].h2,e[i].m2,e[j].h2,e[j].m2-e[j].tim,e[j].h2,e[j].m2))
  202. add(i<<|,j<<),add(j<<|,i<<);
  203. }
  204. tot=;
  205. for(int i=;i<=n;++i)
  206. {
  207. if(!dfn[i<<]) tarjan(i<<);
  208. if(!dfn[i<<|]) tarjan(i<<|);
  209. }
  210. for(int i=;i<=n;++i)
  211. if(id[i<<]==id[i<<|])
  212. {
  213. puts("NO");
  214. return ;
  215. }
  216. rebuild();
  217. topsort();
  218. }
Priest John's Busiest Day
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11007   Accepted: 3759   Special Judge

Description

John is the only priest in his town. September 1st is the John's busiest day in a year because there is an old legend in the town that the couple who get married on that day will be forever blessed by the God of Love. This year N couples plan to get married on the blessed day. The i-th couple plan to hold their wedding from time Si to time Ti. According to the traditions in the town, there must be a special ceremony on which the couple stand before the priest and accept blessings. The i-th couple need Di minutes to finish this ceremony. Moreover, this ceremony must be either at the beginning or the ending of the wedding (i.e. it must be either from Si to Si + Di, or from Ti - Di to Ti). Could you tell John how to arrange his schedule so that he can present at every special ceremonies of the weddings.

Note that John can not be present at two weddings simultaneously.

Input

The first line contains a integer N ( 1 ≤ N ≤ 1000). 
The next N lines contain the SiTi and DiSi and Ti are in the format of hh:mm.

Output

The first line of output contains "YES" or "NO" indicating whether John can be present at every special ceremony. If it is "YES", output another N lines describing the staring time and finishing time of all the ceremonies.

Sample Input

  1. 2
  2. 08:00 09:00 30
  3. 08:15 09:00 20

Sample Output

  1. YES
  2. 08:00 08:30
  3. 08:40 09:00

Source

poj 3686 Priest John's Busiest Day的更多相关文章

  1. 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 ...

  2. POJ 3683 Priest John's Busiest Day (2-SAT)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6900   Accept ...

  3. POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10010   Accep ...

  4. 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 ...

  5. poj - 3683 - Priest John's Busiest Day(2-SAT)

    题意:有N场婚礼,每场婚礼的开始时间为Si,结束时间为Ti,每场婚礼有个仪式,历时Di,这个仪式要么在Si时刻开始,要么在Ti-Di时刻开始,问能否安排每场婚礼举行仪式的时间,使主持人John能参加所 ...

  6. POJ 3683 Priest John's Busiest Day (2-SAT)

    题意:有n对新人要在同一天结婚.结婚时间为Ti到Di,这里有时长为Si的一个仪式需要神父出席.神父可以在Ti-(Ti+Si)这段时间出席也可以在(Di-Si)-Si这段时间.问神父能否出席所有仪式,如 ...

  7. POJ 3683 Priest John's Busiest Day (2-SAT,常规)

    题意: 一些人要在同一天进行婚礼,但是牧师只有1个,每一对夫妻都有一个时间范围[s , e]可供牧师选择,且起码要m分钟才主持完毕,但是要么就在 s 就开始,要么就主持到刚好 e 结束.因为人数太多了 ...

  8. POJ 3683 Priest John's Busiest Day

    2-SAT简单题,判断一下两个开区间是否相交 #include<cstdio> #include<cstring> #include<cmath> #include ...

  9. POJ 3683 Priest John's Busiest Day[2-SAT 构造解]

    题意: $n$对$couple$举行仪式,有两个时间段可以选择,问是否可以不冲突举行完,并求方案 两个时间段选择对应一真一假,对于有时间段冲突冲突的两人按照$2-SAT$的规则连边(把不冲突的时间段连 ...

随机推荐

  1. Redux系列02:一个炒鸡简单的react+redux例子

    前言 在<Redux系列01:从一个简单例子了解action.store.reducer>里面,我们已经对redux的核心概念做了必要的讲解.接下来,同样是通过一个简单的例子,来讲解如何将 ...

  2. Azure 基础:Queue Storage

    Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在前文中介绍了 File Storage 的基本用 ...

  3. Salesforce随笔: 将Visualforce Page渲染为PDF文件(Render a Visualforce Page as a PDF File)

    参照 : Visualforce Developer Guide 第60页 <Render a Visualforce Page as a PDF File> 你可以用PDF渲染服务生成一 ...

  4. 从浏览器输入URL到显示页面到底发生了什么?

    首先说明一下,当系统本地缓存了你所请求的资源时,会直接把缓存内容解析并显示,而不会进行以下的一系列行为. 一.DNS域名解析 至今的计算机数量可谓是数不胜数,而它们的唯一识别身份就是ip地址.我们常说 ...

  5. fiddler之会话数据的修改

    fiddler之会话数据的修改 fiddler记录http的请求,并且针对特定的http请求,可以分析请求数据.修改数据.调试web系统等,功能十分强大.本篇主要讲两种修改的数据的方法,断点和Unlo ...

  6. camscanner(扫描全能王)功能解析与复现

    早就在用camscanner(扫描全能王)这个软件,感觉很不错. 主要功能: 1.页面截取校正 2.增强处理(灰度与颜色) 刚好最近工作与此相关,静心做点仿真,看看其中的操作原理,也做个demo玩玩. ...

  7. 【读书笔记】Linux内核设计与实现(第五章)

    5.1 内核通信 系统调用在用户空间和硬件设备之间添加了一个中间层. 该层主要作用: 1.为用户空间提供了一种硬件的抽象接口. 2.保证了系统的稳定和安全. 3.每个进程都运行在虚拟系统中. 在Lin ...

  8. 暑假学习笔记(一)——初识Neo4j和APICloud入门

    暑假学习笔记(一)--初识Neo4j和APICloud入门 20180719笔记 1.Neo4j 接了学姐的系统测试报告任务,感觉工作很繁重,但是自己却每天挥霍时光.9月份就要提交系统测试报告了,但是 ...

  9. K8S 创建rc 时 不适用本地镜像的解决办法

    spec: containers: - name: nginx image: image: reg.docker.lc/share/nginx:latest imagePullPolicy: IfNo ...

  10. Windows 下安装redis 并且设置开机自动启动的过程.

    1. 下载redis 的 windows下的安装文件 https://github.com/MicrosoftArchive/redis/releasesmsi文件下载地址https://github ...