搜索(DLX):HDU 3663 Power Stations

Power Stations

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2164    Accepted Submission(s): 626
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 5

1 2

2 3

3 1

1 5

1 5

1 5

4 4 5

1 2

2 3

3 4

4 1

1 5

1 5

1 5

1 5

Sample Output

1 5

0 0

0 0

No solution

  就是没看题导致WA1发。

  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 using namespace std;
  5 const int maxn=5010;
  6 const int maxnode=1000010;
  7 int s[maxn],t[maxn],belong[maxn],ans[maxn];
  8 struct DLX{
  9     int L[maxnode],R[maxnode],U[maxnode],D[maxnode];
 10     int cnt,Row[maxnode],Col[maxnode],C[maxn],H[maxn];
 11     void Init(int n,int m){
 12         for(int i=0;i<=m;i++){
 13             L[i]=i-1;R[i]=i+1;
 14             U[i]=D[i]=i;C[i]=0;
 15         }
 16         cnt=m;L[0]=m;R[m]=0;
 17         for(int i=1;i<=n;i++)H[i]=0;
 18     }
 19
 20     void Link(int r,int c){
 21         Row[++cnt]=r;C[Col[cnt]=c]+=1;
 22
 23         U[cnt]=c;D[cnt]=D[c];U[D[c]]=cnt;D[c]=cnt;
 24
 25         if(!H[r])H[r]=L[cnt]=R[cnt]=cnt;
 26         else R[cnt]=R[H[r]],L[cnt]=H[r],L[R[cnt]]=cnt,R[L[cnt]]=cnt;
 27     }
 28
 29     void Delete(int c){
 30         L[R[c]]=L[c];R[L[c]]=R[c];
 31         for(int i=D[c];i!=c;i=D[i])
 32             for(int j=R[i];j!=i;j=R[j])
 33                 --C[Col[j]],U[D[j]]=U[j],D[U[j]]=D[j];
 34     }
 35
 36     void Resume(int c){
 37         L[R[c]]=c;R[L[c]]=c;
 38         for(int i=U[c];i!=c;i=U[i])
 39             for(int j=L[i];j!=i;j=L[j])
 40                 ++C[Col[j]],U[D[j]]=j,D[U[j]]=j;
 41     }
 42
 43     bool Solve(){
 44         if(!R[0])return true;
 45         int p=R[0];
 46         for(int i=R[p];i;i=R[i])
 47             if(C[p]>C[i])
 48                 p=i;
 49
 50         Delete(p);
 51         for(int i=D[p];i!=p;i=D[i]){
 52             if(ans[belong[Row[i]]])continue;
 53             for(int j=R[i];j!=i;j=R[j])
 54                 Delete(Col[j]);
 55
 56             ans[belong[Row[i]]]=Row[i];
 57             if(Solve())
 58                 return true;
 59             ans[belong[Row[i]]]=0;
 60             for(int j=L[i];j!=i;j=L[j])
 61                 Resume(Col[j]);
 62         }
 63         Resume(p);
 64         return false;
 65     }
 66 }dlx;
 67
 68 int L[maxn],R[maxn];
 69 bool G[maxn][maxn];
 70
 71 int main(){
 72     int a,b,N,M,D,tot;
 73     while(scanf("%d%d%d",&N,&M,&D)!=EOF){
 74         memset(G,0,sizeof(G));
 75         while(M--){
 76             scanf("%d%d",&a,&b);
 77             G[a][b]=true;
 78             G[b][a]=true;
 79         }
 80
 81         tot=0;
 82         for(int i=1;i<=N;i++){
 83             scanf("%d%d",&s[i],&t[i]);
 84             tot+=(t[i]-s[i]+1)*(t[i]-s[i]+2)/2;
 85             G[i][i]=true;
 86         }
 87
 88         dlx.Init(tot,N*D);
 89         memset(ans,0,sizeof(ans));
 90         for(int x=1,p=0;x<=N;x++)
 91             for(int l=s[x];l<=t[x];l++)
 92                 for(int r=l;r<=t[x];r++){
 93                     ++p;L[p]=l;R[p]=r;belong[p]=x;
 94                     for(int j=l;j<=r;j++)
 95                         for(int y=1;y<=N;y++)
 96                             if(G[x][y])dlx.Link(p,N*(j-1)+y);
 97                 }
 98         if(dlx.Solve()){
 99             for(int i=1;i<=N;i++)
100                 printf("%d %d\n",L[ans[i]],R[ans[i]]);
101         }
102         else
103             printf("No solution\n");
104         printf("\n");
105     }
106     return 0;
107 }
时间: 2025-01-05 22:13:48

搜索(DLX):HDU 3663 Power Stations的相关文章

[DLX精确覆盖] hdu 3663 Power Stations

题意: 给你n.m.d,代表有n个城市,m条城市之间的关系,每个城市要在日后d天内都有电. 对于每个城市,都有一个发电站,每个发电站可以在[a,b]的每一个连续子区间内发电. x城市发电了,他相邻的城市也有电,并且每个发电站只能启动一次,或者不启动. 现在问,如何安排发电站的启动,保证每个城市d天都有电. 输出发电方案,不发电的话输出0 0 思路: 一个简单的精确覆盖问题,就是建图比较麻烦一点. 这里考虑到每天都要得到电,所以把每个城市每天都设为列(n*d) 然后每个城市对于[a,b]的所有子区

Power Stations HDU - 3663

我为什么T了.... Power Stations Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2517    Accepted Submission(s): 748Special Judge Problem Description There are N towns in our country, and some of them

【搜索】HDU 5348 MZL&#39;s endless loop

通道 题意:给出n个点,m条边,现在要给边定向使得点的出度和入度的差不超过1 思路: 对每个点进行出度和入度的判断,如果出度大,就先进行反向的搜索(每搜索一条边u,v就认为这是一条v到u的有向边),反之,进行正向搜索(每搜到一条边u,v认为这是一条u到v的有向边),一直搜索到找不到边能继续为止,每条边只遍历一次 代码: #pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #inclu

【HDOJ】Power Stations

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 <

搜索(DLX):HOJ 1017 - Exact cover

1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6751 Solved: 3519 Description There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in e

hdu 2406 Power Strings KMP

Power Strings                                                               Time Limit:3000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u Description Given two strings a and b we define a*b to be their concatenation. For example, if a =

【搜索】 HDU 5025 Saving Tang Monk

优先队列+状压蛇+没有了 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <string> #include <iostream> #include <algorithm> #include <sstream> #include <math.h> using namespace std; #include <queue

【搜索】 HDU 5323 Solve this interesting problem

点击打开链接 用线段树方式建树 [ 0, n] 已知[ l, r] 结点 求n 若 建一个[0, 2*r] 的线段树  这是的总点数的奇的,(左子树!=右子树 [0, r]  在左子树里 则n最大为2*r 若 建一个[0, 2*r+1] 的线段树 (左子树==右子树 [0, r]  在左子树里 这时则 [0, r] 就可以建树 所以搜的时候超出2*r 就直接return #include <cstdio> #include <cstring> #include <cstdli

【搜索】 HDU 3533 Escape BFS 预处理

要从0,0 点 跑到m,n点  路上会有k个堡垒发射子弹,有子弹的地方不能走,子弹打到别的堡垒就会消失,或者一直飞出边界(人不能经过堡垒 可以上下左右或者站着不动 每步都需要消耗能量  一共有eng个能量 先预处理出地图 用三维数组表示mp[x][y][time] time表示该时间的地图上储存不能走的点 然后就是普通BFS #include <stdio.h> #include <string.h> #include <stdlib.h> #include <s