DLX(精确覆盖) HDU 3663 Power Stations

来源:互联网 发布:犀牛软件6.0下载 编辑:程序博客网 时间:2024/05/15 06:51

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

 

 

题意:给出村子的相邻关系,每个村子有一个供电站,供电时间是si~ei, 你能够选择其中的一段连续的时间供电,如果一个村子正在供电,跟他相邻的村子也能用电。但是每个村子只能被一个供电站供电。不然会被烧掉。而且每个村子的供电一旦停止了,就不能再次开启了。问怎么样安排能让所有的村子在D天内都有电用。

 

思路:我们设计一下列表示的状态,列表示的是某个村子在某个时间有电用。行的话表示某个村子在[l,r]内主动供电。这样子状态基本设计好了,由于每个村子只能发一次电,所以我们多加n列表示某个村子是否已经发过电了。但是这些列是不一定要覆盖的。 我们同样能用精确覆盖的方法来做。具体请看代码

 

代码:

#include<iostream>#include<cstdio>#include<algorithm>#include<vector>using namespace std;const int maxc = 60 * 6;const int maxn = maxc * 60 * 25;#define FOR(i,s,A) for(int i=A[s];i!=s;i=A[i]) int U[maxn], D[maxn], L[maxn], R[maxn];int S[maxc], cnt;int l[maxn], r[maxn], vge[maxn], col[maxn];inline void LrRemove(int x) { L[R[x]] = L[x]; R[L[x]] = R[x]; }inline void LrResume(int x) { L[R[x]] = R[L[x]] = x; }inline void UdRemove(int x) { U[D[x]] = U[x]; D[U[x]] = D[x]; }inline void UdResume(int x) { U[D[x]] = D[U[x]] = x; }void RemoveColumn(int c){if (c == 3 * 5 + 1)char a = 'a';LrRemove(c);FOR(i, c, D) {FOR(j, i, R) {--S[col[j]];UdRemove(j);}}}void ResumeColumn(int c){FOR(i, c, U) {FOR(j, i, L) {++S[col[j]];UdResume(j);}}LrResume(c);}bool adj[65][65];int si[65], ei[65];vector<int> column;int n, m, d;void add_row(int l, int r, int vge){sort(column.begin(), column.end());int z = unique(column.begin(), column.end()) - column.begin();column.erase(column.begin() + z, column.end());int q = cnt;for (int i = 0; i < column.size(); ++i) {int c = column[i];L[cnt] = cnt - 1; R[cnt] = cnt + 1;U[cnt] = U[c]; D[cnt] = c;D[U[cnt]] = cnt; U[c] = cnt;++S[c];::l[cnt] = l, ::r[cnt] = r, ::vge[cnt] = vge;col[cnt] = c;++cnt;}L[q] = cnt - 1; R[cnt - 1] = q;}void init(){memset(S, 0, sizeof(S));cnt = 0;for (int i = 0; i <= n*d + n; ++i) {L[i] = i - 1; R[i] = i + 1;U[i] = D[i] = i;col[i] = i;++cnt;}L[0] = cnt - 1; R[cnt - 1] = 0;}void input(){init();memset(adj, 0, sizeof(adj));while (m--) {int u, v; scanf("%d%d", &u, &v);adj[u][v] = adj[v][u] = true;}for (int i = 1; i <= n; ++i) adj[i][i] = true;for (int i = 1; i <= n; ++i) scanf("%d%d", si + i, ei + i);for (int i = 1; i <= n; ++i) {for (int s = si[i]; s <= ei[i];++s)for (int e = s; e <= ei[i]; ++e) {column.clear();for (int j = 1; j <= n; ++j) if (adj[i][j]){for (int k = s; k <= e; ++k)column.push_back((j - 1)*d + k);}column.push_back(n*d + i);add_row(s, e, i);}}}int stk[maxn], top;int outl[65], outr[65], outvge[65];bool dfs(){if (R[0] > n*d || R[0]==0)return true;int c = R[0];for (int i = R[0]; i <= n*d&&i != 0; i = R[i]) if (S[c] > S[i]) c = i;if (S[c] == 0) return false;RemoveColumn(c);FOR(i, c, D) {FOR(j, i, R) RemoveColumn(col[j]);//printf("%d %d %d\n", vge[i], l[i], r[i]);stk[top++] = i;if (dfs()) return true;--top;FOR(j, i, L) ResumeColumn(col[j]);}ResumeColumn(c);return false;}void solve(){top = 0;if (!dfs()) printf("No solution\n");else {memset(outl, 0, sizeof(outl));memset(outr, 0, sizeof(outr));memset(outvge, 0, sizeof(outvge));for (int i = 0; i < top; ++i) {int x = stk[i];outl[vge[x]] = l[x];outr[vge[x]] = r[x];}for (int i = 1; i <= n; ++i) printf("%d %d\n", outl[i], outr[i]);}}int main(){while (scanf("%d%d%d", &n, &m, &d) == 3){input();solve();}}


 

 

 

 

0 0
原创粉丝点击