POJ 3683 2-sat问题,不过要输出路径用topsort

来源:互联网 发布:面膜知乎 编辑:程序博客网 时间:2024/05/18 03:13
Priest John's Busiest Day
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 3985 Accepted: 1394 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 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

POJ Founder Monthly Contest – 2008.08.31, Dagger and Facer
2-sat是这样一类问题,每个点有两个选择
然后有一些约束条件,其他的可以去看看一些OI论文。。

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;}
代码长度为201,可见此题非常的彪悍。。
原创粉丝点击