【HDOJ】Power Stations

DLX。针对每个城市,每个城市可充电的区间构成一个plan。每个决策由N*D个时间及N个精确覆盖构成。

 /* 3663 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = ;
const int maxe = ;
int B[maxn];
int S[maxn], E[maxn];
int ut[maxn], vt[maxn];
int N, M, D;
int adj[maxn][maxn];
int arr[maxn]; typedef struct {
static const int maxc = *+;
static const int maxr = *+;
static const int maxn = ***+; int n, sz;
int S[maxc]; int row[maxn], col[maxn];
int L[maxn], R[maxn], U[maxn], D[maxn]; int bound;
int ansd, ans[maxr]; void init(int bound_, int n_) {
bound = bound_;
n = n_; rep(i, , n+) {
L[i] = i - ;
R[i] = i + ;
U[i] = i;
D[i] = i;
col[i] = i;
}
L[] = n;
R[n] = ; sz = n + ;
memset(S, , sizeof(S));
} void addRow(int r, vi columns) {
int first = sz;
int size = SZ(columns); rep(i, , size) {
int c = columns[i]; L[sz] = sz - ;
R[sz] = sz + ; D[sz] = c;
U[sz] = U[c];
D[U[c]] = sz;
U[c] = sz; row[sz] = r;
col[sz] = c; ++S[c];
++sz;
} L[first] = sz - ;
R[sz - ] = first;
} 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];
--S[col[j]];
}
}
} void restore(int c) {
L[R[c]] = c;
R[L[c]] = c;
for (int i=D[c]; i!=c; i=D[i]) {
for (int j=R[i]; j!=i; j=R[j]) {
U[D[j]] = j;
D[U[j]] = j;
++S[col[j]];
}
}
} bool dfs(int d) {
if (R[]== || R[]>bound) {
ansd = d;
return true;
} int c = R[];
for (int i=R[]; i!=&&i<=bound; i=R[i]) {
if (S[i] < S[c])
c = i;
} remove(c);
for (int i=D[c]; i!=c; i=D[i]) {
ans[d] = row[i];
for (int j=R[i]; j!=i; j=R[j]) {
remove(col[j]);
}
if (dfs(d + )) return true;
for (int j=L[i]; j!=i; j=L[j]) {
restore(col[j]);
}
}
restore(c); return false;
} } DLX; DLX solver; int encode(int index, int s, int e) {
return (index<<) + (s<<)+e;
} void decode(int code, int& index, int& s, int& e) {
e = code & ;
code >>= ;
s = code & ;
code >>= ;
index = code;
} void solve() {
int nd = N*D;
solver.init(nd, nd+N); B[] = ;
rep(i, , N+)
B[i] = B[i-]+D; rep(i, , N+) {
int s = S[i];
int e = E[i];
int m = ;
rep(j, , N+) {
if (adj[i][j])
arr[m++] = j;
} rep(ss, s, e+) {
rep(ee, ss, e+) {
vi columns;
rep(k, , m) {
int v = arr[k];
rep(j, ss, ee+)
columns.pb(B[v] + j);
}
columns.pb(nd + i);
solver.addRow(encode(i, ss, ee), columns);
}
}
} bool flag = solver.dfs();
if (flag) {
memset(ut, , sizeof(ut));
memset(vt, , sizeof(vt));
int s, e, index;
rep(i, , solver.ansd) {
decode(solver.ans[i], index, s, e);
ut[index] = s;
vt[index] = e;
}
rep(i, , N+)
printf("%d %d\n", ut[i], vt[i]);
} else {
puts("No solution");
}
putchar('\n');
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int u, v; while (scanf("%d %d %d",&N,&M,&D)!=EOF) {
memset(adj, false, sizeof(adj));
rep(i, , M) {
scanf("%d %d", &u, &v);
adj[u][v] = adj[v][u] = true;
}
rep(i, , N+)
adj[i][i] = true;
rep(i, , N+) {
scanf("%d %d", &S[i], &E[i]);
}
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
上一篇:BZOJ1007: [HNOI2008]水平可见直线(单调栈)


下一篇:Csstyle - 创建简洁、可维护强的 CSS 样式