标签:
DLX。针对每个城市,每个城市可充电的区间构成一个plan。每个决策由N*D个时间及N个精确覆盖构成。
1 /* 3663 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #include <deque> 10 #include <algorithm> 11 #include <cstdio> 12 #include <cmath> 13 #include <ctime> 14 #include <cstring> 15 #include <climits> 16 #include <cctype> 17 #include <cassert> 18 #include <functional> 19 #include <iterator> 20 #include <iomanip> 21 using namespace std; 22 //#pragma comment(linker,"/STACK:102400000,1024000") 23 24 #define sti set<int> 25 #define stpii set<pair<int, int> > 26 #define mpii map<int,int> 27 #define vi vector<int> 28 #define pii pair<int,int> 29 #define vpii vector<pair<int,int> > 30 #define rep(i, a, n) for (int i=a;i<n;++i) 31 #define per(i, a, n) for (int i=n-1;i>=a;--i) 32 #define clr clear 33 #define pb push_back 34 #define mp make_pair 35 #define fir first 36 #define sec second 37 #define all(x) (x).begin(),(x).end() 38 #define SZ(x) ((int)(x).size()) 39 #define lson l, mid, rt<<1 40 #define rson mid+1, r, rt<<1|1 41 42 const int maxn = 65; 43 const int maxe = 305; 44 int B[maxn]; 45 int S[maxn], E[maxn]; 46 int ut[maxn], vt[maxn]; 47 int N, M, D; 48 int adj[maxn][maxn]; 49 int arr[maxn]; 50 51 typedef struct { 52 static const int maxc = 65*6+5; 53 static const int maxr = 60*16+5; 54 static const int maxn = 60*6*60*15+105; 55 56 int n, sz; 57 int S[maxc]; 58 59 int row[maxn], col[maxn]; 60 int L[maxn], R[maxn], U[maxn], D[maxn]; 61 62 int bound; 63 int ansd, ans[maxr]; 64 65 void init(int bound_, int n_) { 66 bound = bound_; 67 n = n_; 68 69 rep(i, 0, n+1) { 70 L[i] = i - 1; 71 R[i] = i + 1; 72 U[i] = i; 73 D[i] = i; 74 col[i] = i; 75 } 76 L[0] = n; 77 R[n] = 0; 78 79 sz = n + 1; 80 memset(S, 0, sizeof(S)); 81 } 82 83 void addRow(int r, vi columns) { 84 int first = sz; 85 int size = SZ(columns); 86 87 rep(i, 0, size) { 88 int c = columns[i]; 89 90 L[sz] = sz - 1; 91 R[sz] = sz + 1; 92 93 D[sz] = c; 94 U[sz] = U[c]; 95 D[U[c]] = sz; 96 U[c] = sz; 97 98 row[sz] = r; 99 col[sz] = c; 100 101 ++S[c]; 102 ++sz; 103 } 104 105 L[first] = sz - 1; 106 R[sz - 1] = first; 107 } 108 109 void remove(int c) { 110 L[R[c]] = L[c]; 111 R[L[c]] = R[c]; 112 for (int i=D[c]; i!=c; i=D[i]) { 113 for (int j=R[i]; j!=i; j=R[j]) { 114 U[D[j]] = U[j]; 115 D[U[j]] = D[j]; 116 --S[col[j]]; 117 } 118 } 119 } 120 121 void restore(int c) { 122 L[R[c]] = c; 123 R[L[c]] = c; 124 for (int i=D[c]; i!=c; i=D[i]) { 125 for (int j=R[i]; j!=i; j=R[j]) { 126 U[D[j]] = j; 127 D[U[j]] = j; 128 ++S[col[j]]; 129 } 130 } 131 } 132 133 bool dfs(int d) { 134 if (R[0]==0 || R[0]>bound) { 135 ansd = d; 136 return true; 137 } 138 139 int c = R[0]; 140 for (int i=R[0]; i!=0&&i<=bound; i=R[i]) { 141 if (S[i] < S[c]) 142 c = i; 143 } 144 145 remove(c); 146 for (int i=D[c]; i!=c; i=D[i]) { 147 ans[d] = row[i]; 148 for (int j=R[i]; j!=i; j=R[j]) { 149 remove(col[j]); 150 } 151 if (dfs(d + 1)) return true; 152 for (int j=L[i]; j!=i; j=L[j]) { 153 restore(col[j]); 154 } 155 } 156 restore(c); 157 158 return false; 159 } 160 161 } DLX; 162 163 DLX solver; 164 165 int encode(int index, int s, int e) { 166 return (index<<6) + (s<<3)+e; 167 } 168 169 void decode(int code, int& index, int& s, int& e) { 170 e = code & 7; 171 code >>= 3; 172 s = code & 7; 173 code >>= 3; 174 index = code; 175 } 176 177 void solve() { 178 int nd = N*D; 179 solver.init(nd, nd+N); 180 181 B[1] = 0; 182 rep(i, 2, N+1) 183 B[i] = B[i-1]+D; 184 185 rep(i, 1, N+1) { 186 int s = S[i]; 187 int e = E[i]; 188 int m = 0; 189 rep(j, 1, N+1) { 190 if (adj[i][j]) 191 arr[m++] = j; 192 } 193 194 rep(ss, s, e+1) { 195 rep(ee, ss, e+1) { 196 vi columns; 197 rep(k, 0, m) { 198 int v = arr[k]; 199 rep(j, ss, ee+1) 200 columns.pb(B[v] + j); 201 } 202 columns.pb(nd + i); 203 solver.addRow(encode(i, ss, ee), columns); 204 } 205 } 206 } 207 208 bool flag = solver.dfs(0); 209 if (flag) { 210 memset(ut, 0, sizeof(ut)); 211 memset(vt, 0, sizeof(vt)); 212 int s, e, index; 213 rep(i, 0, solver.ansd) { 214 decode(solver.ans[i], index, s, e); 215 ut[index] = s; 216 vt[index] = e; 217 } 218 rep(i, 1, N+1) 219 printf("%d %d\n", ut[i], vt[i]); 220 } else { 221 puts("No solution"); 222 } 223 putchar(‘\n‘); 224 } 225 226 int main() { 227 ios::sync_with_stdio(false); 228 #ifndef ONLINE_JUDGE 229 freopen("data.in", "r", stdin); 230 freopen("data.out", "w", stdout); 231 #endif 232 233 int u, v; 234 235 while (scanf("%d %d %d",&N,&M,&D)!=EOF) { 236 memset(adj, false, sizeof(adj)); 237 rep(i, 0, M) { 238 scanf("%d %d", &u, &v); 239 adj[u][v] = adj[v][u] = true; 240 } 241 rep(i, 1, N+1) 242 adj[i][i] = true; 243 rep(i, 1, N+1) { 244 scanf("%d %d", &S[i], &E[i]); 245 } 246 solve(); 247 } 248 249 #ifndef ONLINE_JUDGE 250 printf("time = %d.\n", (int)clock()); 251 #endif 252 253 return 0; 254 }
标签:
原文地址:http://www.cnblogs.com/bombe1013/p/4981199.html