POJ 3683 2-sat问题,不过要输出路径用topsort
来源:互联网 发布:面膜知乎 编辑:程序博客网 时间:2024/05/18 03:13
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 Si, Ti and Di. Si 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
208:00 09:00 3008:15 09:00 20
Sample Output
YES08:00 08:3008:40 09:00
Source
Source Code
Problem: 3683 User: athenaaMemory: 11768K Time: 391MSLanguage: C++ Result: Accepted- Source Code
#include<stdio.h>#include<algorithm>#include<vector>#include<queue>#include<stack>using namespace std;struct node{int s;int e;int invial;};node time[1005];int belong[2005],low[2005],dfn[2005],in[2005],cet[2005],col[2005];bool used[2005],instack[2005];int index,cnt,n;vector<int>map[2005];bool net[2005][2005];stack<int>s;void init(){int i;for(i=0;i<2005;i++){map[i].clear();//net[i].clear();}memset(net,0,sizeof(net));memset(in,0,sizeof(in));memset(col,0,sizeof(col));memset(belong,0,sizeof(belong));memset(used,0,sizeof(used));memset(instack,0,sizeof(instack));memset(low,0,sizeof(low));memset(dfn,-1,sizeof(dfn));memset(net,0,sizeof(net));index=0;cnt=0;}bool judge(int s1,int e1,int s2,int e2){if(s2<e1&&s1<e2)return true;return false;}int min(int a,int b){if(a>b)return b;elsereturn a;}void tarjan(int u){int i,v;index++;dfn[u]=index;low[u]=index;instack[u]=true;used[u]=true;s.push(u);for(i=0;i<map[u].size();i++){v=map[u][i];if(!used[v]){tarjan(v);low[u]=min(low[u],low[v]);}else if(instack[v]){low[u]=min(low[u],dfn[v]);}}if(dfn[u]==low[u]){cnt++;do{v=s.top();s.pop();belong[v]=cnt;instack[v]=false;}while(u!=v);}}void topsort(){int i,u,v;queue<int>q;for(i=1;i<=cnt;i++)if(in[i]==0)q.push(i);while(!q.empty()){u=q.front();q.pop();if(!col[u]){col[u]=1;col[cet[u]]=-1;}for(v=1;v<=cnt;v++){if(net[u][v]){net[u][v]=false;in[v]--;if(in[v]==0)q.push(v);}}}}int main(){int i,a,b,c,d,l,j;init();scanf("%d",&n);for(i=0;i<n;i++){scanf("%d:%d %d:%d %d",&a,&b,&c,&d,&l);time[i].s=a*60+b;time[i].e=c*60+d;time[i].invial=l;}for(i=0;i<n;i++)for(j=0;j<n;j++){if(i==j)continue;if(judge(time[i].s,time[i].s+time[i].invial,time[j].s,time[j].s+time[j].invial)){map[i].push_back(j+n);//printf("%d %d/n",i,j+n);}if(judge(time[i].s,time[i].s+time[i].invial,time[j].e-time[j].invial,time[j].e)){map[i].push_back(j);//printf("%d %d/n",i,j);}if(judge(time[i].e-time[i].invial,time[i].e,time[j].s,time[j].s+time[j].invial)){map[i+n].push_back(j+n);//printf("%d %d/n",i+n,j+n);}if(judge(time[i].e-time[i].invial,time[i].e,time[j].e-time[j].invial,time[j].e)){map[i+n].push_back(j);//printf("%d %d/n",i+n,j);}}for(i=0;i<2*n;i++){if(dfn[i]==-1)tarjan(i);}for(i=0;i<n;i++){if(belong[i]==belong[i+n]){printf("NO/n");return 0;}cet[belong[i]]=belong[i+n];cet[belong[i+n]]=belong[i];}printf("YES/n");for(i=0;i<2*n;i++)for(j=0;j<map[i].size();j++)if(belong[i]!=belong[map[i][j]])net[belong[map[i][j]]][belong[i]]=true;for(i=1;i<=cnt;i++)for(j=1;j<=cnt;j++){if(net[i][j])in[j]++;}topsort();for(i=0;i<n;i++){if(col[belong[i]]==1){printf("%02d:%02d %02d:%02d/n",time[i].s/60,time[i].s%60,(time[i].s+time[i].invial)/60,(time[i].s+time[i].invial)%60);}else{printf("%02d:%02d %02d:%02d/n",(time[i].e-time[i].invial)/60,(time[i].e-time[i].invial)%60,time[i].e/60,time[i].e%60);}}return 0;}
- POJ 3683 2-sat问题,不过要输出路径用topsort
- [2-sat][topsort输出解] POJ 3648 Wedding
- poj 3648 2-SAT建图+topsort输出结果
- poj 3683 2-sat问题,输出任意一组可行解
- POJ 3648Wedding (2-SAT 输出路径)
- poj 3683 Priest John's Busiest Day(2-SAT 输出路径)
- POJ 3683 Priest John's Busiest Day (2-SAT 输出路径)
- poj 3683 2-SAT 拓扑排序输出
- POJ 3683 2-sat 输出解
- POJ 3683 2-SAT +输出方案
- POJ 3683 2-SAT 输出可行方案
- 2-sat 问题 总结 例题poj 3683
- poj 3683 2-sat建图+拓扑排序输出结果
- poj 3683(2-sat+输出一组可行解)
- poj 3683 Priest John's Busiest Day 2-SAT输出
- POJ 3684 Priest John's Busiest Day 2-SAT+输出路径
- poj 3683【2-SAT】
- poj 3683 2-sat
- 怎么组装电脑
- hdu1301 Jungle Roads
- Android平台开发指导(Android Porting Guide)(一)
- pz之1205
- 特点一:0配置
- POJ 3683 2-sat问题,不过要输出路径用topsort
- 栈学习---数组实现
- 恶意注册引发的思考
- 用BasicScrollBarUI实现一个简单的个性化滚动条皮肤
- 软件开发与软件测试
- 双向链表
- 有几件事情要做到
- symbian学习笔记
- android的logcat详细用法