HDU 3663 Power Stations 解题报告(Dancing Link)

来源:互联网 发布:软件正版化实施方案 编辑:程序博客网 时间:2024/05/22 03:10

Power Stations

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1735    Accepted Submission(s): 472
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 51 22 33 11 51 51 54 4 51 22 33 44 11 51 51 51 5
 

Sample Output
1 50 00 0No solution
 

Source
2010 Asia Regional Harbin
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  3669 3666 3668 3665 3664 
 

    解题报告: Dancing Link题。
    主要是建模。每列记录每个村子每天的用电,以及该村子是否在供电,每行记录区间[l, r]时间是每个村子的供电。这个转化为了准确覆盖的问题。
    唯一要注意的地方是……数据中有重复边,邻接表RE到死,一直加空间都没用……用矩阵就好了。代码如下:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int M=4000*1000;const int N=5000;int l[M], r[M], d[M], u[M], col[M], row[M], h[N], control[N], res[N];int dcnt = 0;inline void addnode(int &x){++x;r[x]=l[x]=u[x]=d[x]=x;}inline void insert_row(int rowx, int x){r[l[rowx]]=x;l[x]=l[rowx];r[x]=rowx;l[rowx]=x;}inline void insert_col(int colx, int x){d[u[colx]]=x;u[x]=u[colx];d[x]=colx;u[colx]=x;}void dlx_init(int cols){memset(h, -1, sizeof(h));memset(control, 0, sizeof(control));dcnt=-1;addnode(dcnt);for(int i=1;i<=cols;++i){addnode(dcnt);insert_row(0, dcnt);}}void remove(int c){l[r[c]]=l[c];r[l[c]]=r[c];for(int i=d[c];i!=c;i=d[i])for(int j=r[i];j!=i;j=r[j]){u[d[j]]=u[j];d[u[j]]=d[j];control[col[j]]--;}}void resume(int c){for(int i=u[c];i!=c;i=u[i])for(int j=l[i];j!=i;j=l[j]){u[d[j]]=j;d[u[j]]=j;control[col[j]]++;}l[r[c]]=c;r[l[c]]=c;}bool DLX(int deep){if(r[0]==0){sort(res, res+deep);for(int i=0;i<deep;i++){res[i]%=60;res[i]--;printf("%d %d\n", res[i]/5, res[i]%5+1);}return true;}int min=M, tempc;for(int i=r[0];i!=0;i=r[i]) if(control[i]<min){min=control[i];tempc=i;}remove(tempc);for(int i=d[tempc];i!=tempc;i=d[i]){res[deep]=row[i];for(int j=r[i];j!=i;j=r[j]) remove(col[j]);if(DLX(deep+1)) return true;for(int j=l[i];j!=i;j=l[j]) resume(col[j]);}resume(tempc);return false;}inline void insert_node(int x, int y){control[y]++;addnode(dcnt);row[dcnt]=x;col[dcnt]=y;insert_col(y, dcnt);if(h[x]==-1)h[x]=dcnt;elseinsert_row(h[x], dcnt);}bool edge[100][100];int main(){#ifdef ACMfreopen("in.txt", "r", stdin);#endifint n, m, d;while(~scanf("%d%d%d", &n, &m, &d)){dlx_init(n*d+n);memset(edge, 0, sizeof(edge));while(m--){int u, v;scanf("%d%d", &u, &v);edge[u][v]=edge[v][u]=true;}for(int i=1;i<=n;i++){int sta, end;scanf("%d%d", &sta, &end);insert_node(i*60, n*d+i);for(int s=sta;s<=end;s++) for(int e=s;e<=end;e++){insert_node(i*60+s*5+e, n*d+i);for(int k=s;k<=e;k++){insert_node(i*60+s*5+e, i*d+k-d);for(int ee=1;ee<=n;ee++) if(edge[i][ee]){insert_node(i*60+s*5+e, ee*d+k-d);}}}}if(!DLX(0)){puts("No solution");}puts("");}}


0 0