题目:
http://acm.hdu.edu.cn/showproblem.php?pid=3663
题意:
有n个城市,有m对直接相连关系,每个城市有一个发电站,每个发电站都有一个限定的工作时间,只能在限定的时间内使用且一旦关闭就不能再使用,每个发电站只能向自己和直接相连的城市送电,每个城市只能接受一个发电站的输送的电,否则就完蛋。选择一个发电站工作方案,在d天内使所有城市都有电,不能满足则输出No solution
思路:
DLX精确覆盖问题。把每个城市的每天的电是一个状态,那么有n∗d列,又每个发电站只能工作一个连续的时间段,意味每个城市只能被选一次开启发电站,因此再加n列标识选择了哪个城市,共n∗(d+1)列。对于d,有d∗(d+1)/2个不同的连续子区间,意味着状态矩阵有n∗d∗(d+1)/2行,把相应的位置标为1,直接跑DLX精确覆盖
#include <bits/stdc++.h>
using namespace std;
const int X = 500000 + 10, N = 1000 + 10, M = 400 + 10, INF = 0x3f3f3f3f;
struct edge
{
int to, next;
}g[N*N*2];
int cnt, head[N];
int n, m, d;
int ts[N], te[N];
bool vis[N][N];
void add_edge(int v, int u)
{
g[cnt].to = u, g[cnt].next = head[v], head[v] = cnt++;
}
struct DLX
{
int U[X], D[X], L[X], R[X], row[X], col[X];
int H[N], S[M];
int head, sz, tot, n, m, ans[N];
void init(int _n, int _m)
{
n = _n, m = _m;
for(int i = 0; i <= m; i++)
L[i] = i-1, R[i] = i+1, U[i] = D[i] = i, S[i] = 0;
head = 0, tot = 0, sz = m;
L[head] = m, R[m] = head;
for(int i = 1; i <= n; i++) H[i] = -1;
}
void link(int r, int c)
{
++S[col[++sz]=c];
row[sz] = r;
D[sz] = D[c], U[D[c]] = sz;
U[sz] = c, D[c] = sz;
if(H[r] < 0) H[r] = L[sz] = R[sz] = sz;
else R[sz] = R[H[r]], L[R[H[r]]] = sz, L[sz] = H[r], R[H[r]] = sz;
}
void del(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])
D[U[j]] = D[j], U[D[j]] = U[j], --S[col[j]];
}
void recover(int c)
{
for(int i = U[c]; i != c; i = U[i])
for(int j = L[i]; j != i; j = L[j])
D[U[j]] = U[D[j]] = j, ++S[col[j]];
R[L[c]] = L[R[c]] = c;
}
bool dance(int dep)
{
if(R[head] == head)
{
tot = dep-1; return true;
}
int c = R[head];
for(int i = R[head]; i != head; i = R[i])
if(S[i] < S[c]) c = i;
del(c);
for(int i = D[c]; i != c; i = D[i])
{
ans[dep] = row[i];
for(int j = R[i]; j != i; j = R[j]) del(col[j]);
if(dance(dep + 1)) return true;
for(int j = L[i]; j != i; j = L[j]) recover(col[j]);
}
recover(c);
return false;
}
}dlx;
void work(int x, int s, int e, int &id)
{
id++;
ts[id] = 0, te[id] = 0;
dlx.link(id, n*d + x);
for(int i = s; i <= e; i++)
for(int j = i; j <= e; j++)
{
id++;
ts[id] = i, te[id] = j;
for(int k = head[x]; k != -1; k = g[k].next)
{
int v = g[k].to;
for(int l = i; l <= j; l++)
dlx.link(id, (l-1)*n + v);
}
dlx.link(id, n*d + x);
}
}
int main()
{
while(~ scanf("%d%d%d", &n, &m, &d))
{
cnt = 0;
memset(head, -1, sizeof head);
memset(vis, 0, sizeof vis);
memset(ts, 0, sizeof ts);
memset(te, 0, sizeof te);
dlx.init(n * (d*(d+1)/2+1), n * (d+1));
int v, u;
for(int i = 1; i <= m; i++)
{
scanf("%d%d", &v, &u);
vis[v][u] = vis[u][v] = true;
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(i == j || vis[i][j]) add_edge(i, j);
int s, e, id = 0;
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &s, &e);
work(i, s, e, id);
}
bool flag = dlx.dance(1);
if(flag == false) puts("No solution");
else
{
sort(dlx.ans + 1, dlx.ans + 1 + dlx.tot);
for(int i = 1; i <= dlx.tot; i++) printf("%d %d\n", ts[dlx.ans[i]], te[dlx.ans[i]]);
}
printf("\n");
}
return 0;
}