题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=3663

Power Stations

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 603    Accepted Submission(s): 168
Special Judge


Problem Description
There are N towns in our country, and some of them are connected by electricity cables. It is known that every town owns a power station. When a town’s power station begins to work, it will provide electric power for this town and the neighboring towns which are connected by cables directly to this town. However, there are some strange bugs in the electric system –One town can only receive electric power from no more than one power station, otherwise the cables will be burned out for overload.

The power stations cannot work all the time. For each station there is an available time range. For example, the power station located on Town 1 may be available from the third day to the fifth day, while the power station on Town 2 may be available from the first day to the forth day. You can choose a sub-range of the available range as the working time for each station. Note that you can only choose one sub-range for each available range, that is, once the station stops working, you cannot restart it again. Of course, it is possible not to use any of them.

Now you are given all the information about the cable connection between the towns, and all the power stations’ available time. You need to find out a schedule that every town will get the electricity supply for next D days, one and only one supplier for one town at any time.
 


 

Input
There are several test cases. The first line of each test case contains three integers, N, M and D (1 <= N <= 60, 1 <= M <= 150, 1 <= D <= 5), indicating the number of towns is N, the number of cables is M, and you should plan for the next D days.

Each of the next M lines contains two integers a, b (1 <= a, b <= N), which means that Town a and Town b are connected directly. Then N lines followed, each contains two numbers si and ei, (1 <= si <= ei <= D) indicating that the available time of Town i’s power station is from the si-th day to the ei-th day (inclusive).
 


 

Output
For each test case, if the plan exists, output N lines. The i-th line should contain two integers ui and vi, indicating that Town i’s power station should work from the ui-th day to vi-day (inclusive). If you didn’t use this power station at all, set ui = vi = 0.

If the plan doesn’t exist, output one line contains “No solution” instead.

Note that the answer may not be unique. Any correct answers will be OK.

Output a blank line after each case.
 


 

Sample Input
3 3 5 1 2 2 3 3 1 1 5 1 5 1 5 4 4 5 1 2 2 3 3 4 4 1 1 5 1 5 1 5 1 5
 


 

Sample Output
1 5 0 0 0 0 No solution
题目大意:给你一个最多60个点150个边的无向图,每个点是一个村庄,每个村庄都有一个发电站,每个电站可以给它所在的村庄和它有边直接连接的所有村庄供电,
现在让你选出一些电站,使每个村庄都能被供电且每个村庄只被一个电站供电。另外,每个村庄的发电站都只能在1-d天内的一个子区间工作,
你需要安排它在哪几天来工作,且每个电站一旦工作结束就不能再次启动,即它工作的时间是连续的。d小于等于5。
思路:Dancing Links考察的关键也是建图,你需要清楚怎么建行和列。 这道题 需要 以每个村庄的每一天为列, 每个村庄的所取的区间为行。 这个区间是题目所给区间的所有子区间 。
因为每一个村庄只能选择一次,所有要加n列进行限制,最后需要建n*d+n列,之后精确覆盖就ok了!
4359032 2011-08-08 11:23:38 Accepted 3663 46MS 2012K 2855 B C++ zyzamp
由于我用的是二维数组即邻接矩阵标记的,所以就去掉了重边的情况!
code:
View Code
  1 /*Problem : 3663 ( Power Stations )     Judge Status : Accepted
2 RunId : 4359032 Language : C++ Author : zhuyawei
3 Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta*/
4 # include<stdio.h>
5 # include<string.h>
6 # define RR 1000
7 # define CC 400
8 # define V RR*CC
9 int U[V],D[V];
10 int L[V],R[V];
11 int C[V];
12 int S[CC],H[RR];
13 int OK[65],mark[65][65];
14 int visit[V];
15 int n,m,d,size,flag,ak;
16 int dir[6]={1,2,4,7,11,16};
17 struct node{
18 int st,ed;
19 }s[155],s3[65];
20 struct node1{
21 int num,st,ed;
22 }s1[RR];
23 struct node2{
24 int num,day;
25 }s2[CC];
26 void Link(int r,int c)
27 {
28 S[c]++;C[size]=c;
29 visit[size]=r;
30 U[size]=U[c];D[U[c]]=size;
31 D[size]=c;U[c]=size;
32 if(H[r]==-1) H[r]=L[size]=R[size]=size;
33 else
34 {
35 L[size]=L[H[r]];R[L[H[r]]]=size;
36 R[size]=H[r];L[H[r]]=size;
37 }
38 size++;
39 }
40 void remove(int c)
41 {
42 int i,j;
43 L[R[c]]=L[c];
44 R[L[c]]=R[c];
45 for(i=D[c];i!=c;i=D[i])
46 {
47 for(j=R[i];j!=i;j=R[j])
48 {
49 S[C[j]]--;
50 U[D[j]]=U[j];
51 D[U[j]]=D[j];
52 }
53 }
54 }
55 void resume(int c)
56 {
57 int i,j;
58 for(i=U[c];i!=c;i=U[i])
59 {
60 for(j=L[i];j!=i;j=L[j])
61 {
62 S[C[j]]++;
63 U[D[j]]=D[U[j]]=j;
64 }
65 }
66 L[R[c]]=R[L[c]]=c;
67 }
68 void Dance(int k)
69 {
70 int i,j,c,Min;
71 if(!R[0])
72 {
73 ak=k;
74 flag=1;
75 return;
76 }
77 for(Min=RR,i=R[0];i;i=R[i])
78 if(Min>S[i]) Min=S[i],c=i;
79 remove(c);
80 for(i=D[c];i!=c;i=D[i])
81 {
82 OK[k]=i;
83 for(j=R[i];j!=i;j=R[j])
84 remove(C[j]);
85 Dance(k+1);
86 if(flag) return;
87 for(j=L[i];j!=i;j=L[j])
88 resume(C[j]);
89 }
90 resume(c);
91 }
92 int main()
93 {
94 int i,j,i1,i2,ans1,ans2,rr,cc,r,ans,a,b;
95 while(scanf("%d%d%d",&n,&m,&d)!=EOF)
96 {
97 memset(mark,0,sizeof(mark));
98 for(i=1;i<=m;i++)
99 {
100 scanf("%d%d",&a,&b);
101 mark[a][b]=mark[b][a]=1;
102 }
103 for(i=1;i<=n;i++)
104 mark[i][i]=1;
105 for(i=1;i<=n;i++)
106 scanf("%d%d",&s[i].st,&s[i].ed);
107 memset(H,-1,sizeof(H));
108 memset(visit,0,sizeof(visit));
109 cc=0;
110 for(i=1;i<=n;i++)
111 {
112 for(j=1;j<=d;j++)
113 {
114 s2[++cc].num=i;
115 s2[cc].day=j;
116 }
117 }
118 cc+=n;
119 for(i=0;i<=cc;i++)
120 {
121 S[i]=0;
122 U[i]=D[i]=i;
123 L[i+1]=i;R[i]=i+1;
124 }R[cc]=0;
125 size=cc+1;
126 rr=0;
127 for(i=1;i<=n;i++)
128 {
129 for(i1=1;i1<=d;i1++)
130 for(i2=i1;i2<=d;i2++)
131 {
132 s1[++rr].num=i;
133 s1[rr].st=i1;
134 s1[rr].ed=i2;
135 }
136 s1[++rr].num=i;
137 s1[rr].st=0;
138 s1[rr].ed=0;
139 }
140 for(i=1;i<=rr;i++)
141 {
142 for(j=1;j<=cc;j++)
143 {
144 ans1=s1[i].num;
145 if(!(s1[i].st>=s[ans1].st && s1[i].ed<=s[ans1].ed) && s1[i].st!=0) continue;
146 if(j<=n*d)
147 {
148 ans2=s2[j].num;
149 if(s2[j].day>=s1[i].st && s2[j].day<=s1[i].ed && mark[ans1][ans2]) Link(i,j);
150 }
151 else
152 {
153 if(ans1==j-n*d) Link(i,j);
154 }
155 }
156 }
157 ak=65;
<s
posted on 2011-08-08 15:01  奋斗青春  阅读(761)  评论(0编辑  收藏  举报