hdu 3663(DLX)

貌似是一道区域赛的题目。 和数独有着相似之处。。。

题目的关键在于建图, 然后要注意的是重边!

Power Stations

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1566    Accepted Submission(s): 409 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
 
Source
 
Recommend
lcy
 
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define N 500000
#define INF 0x3fffffff

struct time
{
    int from,to,n;
}save[66],ans[1000];

int map[66][66];
int U[N],R[N],D[N],L[N],num[N],H[N],col[N],line[N];
int head,id;
int nn,mm,flag;
int n,m,d;
int path[1000],end[66];



void prepare()
{
    for(int i=0;i<=mm;i++)
    {
        num[i]=0;
        U[i]=i;
        D[i]=i;
        R[i]=i+1;
        L[i+1]=i;
    }
    R[mm]=0;
    L[0]=mm;
    memset(H,-1,sizeof(H));
}

void link(int tn,int tm)
{
    id++;
    num[ line[id]=tm ]++;
    col[id]=tn;
    U[ D[tm] ]=id;
    D[id]=D[tm];
    U[id]=tm;
    D[tm]=id;
    if(H[tn]<0) H[tn]=R[id]=L[id]=id;
    else
    {
        L[ R[H[tn]] ]=id;
        R[id] = R[ H[tn] ];
        L[id]=H[tn];
        R[ H[tn] ]=id;
    }
}

void build()
{
    id=mm;
    prepare();
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        for(int i1=save[i].from;i1<=save[i].to;i1++)
            for(int j1=i1;j1<=save[i].to;j1++)
            {
                ++sum;
                link(sum,i);
                ans[sum].from=i1; ans[sum].to=j1; ans[sum].n=i;
                for(int k=i1;k<=j1;k++)
                {
                    link(sum,n+(i-1)*d+k);
                    for(int p=1;p<=n;p++)
                    {
                        if(map[i][p]!=1) continue;
                        link(sum,n+(p-1)*d+k);
                    }
                }
            }
        sum++;
        link(sum,i);
        ans[sum].from=0; ans[sum].to=0; ans[sum].n=i;
    }
}

void remove(int s)
{
    L[R[s]]=L[s];
    R[L[s]]=R[s];
    for(int i=D[s];i!=s;i=D[i])
    {
        for(int j=R[i];j!=i;j=R[j])
        {
            U[D[j]]=U[j];
            D[U[j]]=D[j];
            num[ line[j] ]--;
        }
    }
}

void resume(int s)
{
    L[R[s]]=R[L[s]]=s;
    for(int i=U[s];i!=s;i=U[i])
        for(int j=L[i];j!=i;j=L[j])
        {
            U[D[j]]=D[U[j]]=j;
            num[line[j]]++;
        }
}

int h()
{
    int ff=0;
    for(int i=R[head];i!=head;i=R[i])
    {
        if(D[i]==i)
        {
            ff=1;
            break;
        }
    }
    if(ff==1) return 0;
    else return 1;
}
void dfs(int s)
{
    if(flag==1) return ;
    if(R[head]==head)
    {
        flag=1;
        for(int i=0;i<s;i++)
            end[ ans[ path[i] ].n ]=path[i];
        return ;
    }
    if( h()==0 ) return ;
    int tmi=INF,tu;
    for(int i=R[head];i!=head;i=R[i])
        if(num[i]<tmi)
        {
            tmi=num[i];
            tu=i;
        }
    remove(tu);
    for(int i=D[tu];i!=tu;i=D[i])
    {
        for(int j=R[i];j!=i;j=R[j])
            remove(line[j]);
        path[s]=col[i];
        dfs(s+1);
        for(int j=L[i];j!=i;j=L[j])
            resume( line[j] );
    }
    resume(tu);
}

int main()
{
    while(scanf("%d%d%d",&n,&m,&d)!=EOF)
    {
        flag=0;
        memset(map,0,sizeof(map));
        mm = n*d+n;
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            map[x][y]=map[y][x]=1;
        }
        for(int i=1;i<=n;i++) scanf("%d%d",&save[i].from,&save[i].to);
        build();
        dfs(0);
        if(flag==0) printf("No solution\n");
        else
        {
            for(int i=1;i<=n;i++)
                printf("%d %d\n",ans[ end[i] ].from,ans[ end[i] ].to );
        }
        printf("\n");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/chenhuan001/p/3008985.html