您的位置:首页 > 其它

搜索(DLX):HDU 3663 Power Stations

2016-06-12 19:29 369 查看

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


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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).

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

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

[align=left]Sample Output[/align]
[align=left] [/align]

1 5

0 0

0 0

No solution

  就是没看题导致WA1发。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=5010;
const int maxnode=1000010;
int s[maxn],t[maxn],belong[maxn],ans[maxn];
struct DLX{
int L[maxnode],R[maxnode],U[maxnode],D[maxnode];
int cnt,Row[maxnode],Col[maxnode],C[maxn],H[maxn];
void Init(int n,int m){
for(int i=0;i<=m;i++){
L[i]=i-1;R[i]=i+1;
U[i]=D[i]=i;C[i]=0;
}
cnt=m;L[0]=m;R[m]=0;
for(int i=1;i<=n;i++)H[i]=0;
}

void Link(int r,int c){
Row[++cnt]=r;C[Col[cnt]=c]+=1;

U[cnt]=c;D[cnt]=D[c];U[D[c]]=cnt;D[c]=cnt;

if(!H[r])H[r]=L[cnt]=R[cnt]=cnt;
else R[cnt]=R[H[r]],L[cnt]=H[r],L[R[cnt]]=cnt,R[L[cnt]]=cnt;
}

void Delete(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])
--C[Col[j]],U[D[j]]=U[j],D[U[j]]=D[j];
}

void Resume(int c){
L[R[c]]=c;R[L[c]]=c;
for(int i=U[c];i!=c;i=U[i])
for(int j=L[i];j!=i;j=L[j])
++C[Col[j]],U[D[j]]=j,D[U[j]]=j;
}

bool Solve(){
if(!R[0])return true;
int p=R[0];
for(int i=R[p];i;i=R[i])
if(C[p]>C[i])
p=i;

Delete(p);
for(int i=D[p];i!=p;i=D[i]){
if(ans[belong[Row[i]]])continue;
for(int j=R[i];j!=i;j=R[j])
Delete(Col[j]);

ans[belong[Row[i]]]=Row[i];
if(Solve())
return true;
ans[belong[Row[i]]]=0;
for(int j=L[i];j!=i;j=L[j])
Resume(Col[j]);
}
Resume(p);
return false;
}
}dlx;

int L[maxn],R[maxn];
bool G[maxn][maxn];

int main(){
int a,b,N,M,D,tot;
while(scanf("%d%d%d",&N,&M,&D)!=EOF){
memset(G,0,sizeof(G));
while(M--){
scanf("%d%d",&a,&b);
G[a][b]=true;
G[b][a]=true;
}

tot=0;
for(int i=1;i<=N;i++){
scanf("%d%d",&s[i],&t[i]);
tot+=(t[i]-s[i]+1)*(t[i]-s[i]+2)/2;
G[i][i]=true;
}

dlx.Init(tot,N*D);
memset(ans,0,sizeof(ans));
for(int x=1,p=0;x<=N;x++)
for(int l=s[x];l<=t[x];l++)
for(int r=l;r<=t[x];r++){
++p;L[p]=l;R[p]=r;belong[p]=x;
for(int j=l;j<=r;j++)
for(int y=1;y<=N;y++)
if(G[x][y])dlx.Link(p,N*(j-1)+y);
}
if(dlx.Solve()){
for(int i=1;i<=N;i++)
printf("%d %d\n",L[ans[i]],R[ans[i]]);
}
else
printf("No solution\n");
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: