「POJ3683」Priest John’s Busiest Day
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
1 2 3 |
2 08:00 09:00 30 08:15 09:00 20 |
Sample Output
1 2 3 |
YES 08:00 08:30 08:40 09:00 |
题解
有n对新人要举行仪式,每对都有两个时间段可以选择,问是否可以所有新人的仪式时间不重叠
若两个时间段重叠则不能同时选TAT
缩点后 拓扑排序得出方案
按输入顺序输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<cstring> #define inf 2000000000 #define ll long long #define mod 1000000007 using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,cnt,top,scc,ind; int a[2005],b[2005],belong[2005],op[2005]; int dfn[2005],low[2005],q[2005]; bool inq[2005]; struct edge{int to,next;}e[2000005],ed[2000005]; int last[2005],last2[2005],d[2005]; int color[2005]; bool jud(int x,int y) { if(b[x]<=a[y]||a[x]>=b[y])return 0; return 1; } void insert(int u,int v) { e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt; } void insert2(int u,int v) { d[v]++; ed[++cnt].to=v;ed[cnt].next=last2[u];last2[u]=cnt; } void build() { for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) { if(jud(2*i,2*j)) { insert(2*i,2*j-1); insert(2*j,2*i-1); } if(jud(2*i,2*j-1)) { insert(2*i,2*j); insert(2*j-1,2*i-1); } if(jud(2*i-1,2*j)) { insert(2*i-1,2*j-1); insert(2*j,2*i); } if(jud(2*i-1,2*j-1)) { insert(2*i-1,2*j); insert(2*j-1,2*i); } } } void tarjan(int x) { dfn[x]=low[x]=++ind; q[++top]=x;inq[x]=1; for(int i=last[x];i;i=e[i].next) if(!dfn[e[i].to]) { tarjan(e[i].to); low[x]=min(low[e[i].to],low[x]); } else if(inq[e[i].to]) low[x]=min(dfn[e[i].to],low[x]); if(low[x]==dfn[x]) { int now=0;scc++; while(now!=x) { now=q[top--]; inq[now]=0; belong[now]=scc; } } } void rebuild() { cnt=0; for(int x=1;x<=2*n;x++) for(int i=last[x];i;i=e[i].next) if(belong[x]!=belong[e[i].to]) insert2(belong[e[i].to],belong[x]); } void dfs(int x) { if(color[x])return; color[x]=-1; for(int i=last2[x];i;i=ed[i].next) dfs(ed[i].to); } void topsort() { for(int i=1;i<=scc;i++) if(!d[i])q[++top]=i; while(top) { int now=q[top--]; if(color[now])continue; color[now]=1;dfs(op[now]); for(int i=last2[now];i;i=ed[i].next) { d[ed[i].to]--; if(!d[ed[i].to])q[++top]=ed[i].to; } } } void print(int x) { printf("%.2d:",x/60); printf("%.2d ",x%60); } int main() { n=read(); int x; for(int i=1;i<=n;i++) { a[2*i]=read(); a[2*i]=a[2*i]*60+read(); b[2*i-1]=read(); b[2*i-1]=b[2*i-1]*60+read(); x=read(); b[2*i]=a[2*i]+x;a[2*i-1]=b[2*i-1]-x; } build(); for(int i=1;i<=2*n;i++) if(!dfn[i])tarjan(i); for(int i=1;i<=n;i++) if(belong[2*i]==belong[2*i-1]) { puts("NO");return 0; } puts("YES"); rebuild(); for(int i=1;i<=n;i++) { op[belong[2*i]]=belong[2*i-1]; op[belong[2*i-1]]=belong[2*i]; } topsort(); for(int i=1;i<=n;i++) if(color[belong[2*i]]==1) print(a[2*i]),print(b[2*i]),puts(""); else print(a[2*i-1]),print(b[2*i-1]),puts(""); return 0; } |
我版本跟你们不一样吗……我咋没有这题
噢是poj。。。