现在的位置: 首页 > 综合 > 正文

HDU 3663 dancing links优化搜索

2019年03月18日 ⁄ 综合 ⁄ 共 4824字 ⁄ 字号 评论关闭

Power Stations

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



Statistic | Submit | Discuss | Note








特别注意。。。这题有重边。因为这个让我RE得蛋都残了。。。














然后就是拍dancing links的模版啦







#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
const int N=60*20*60*6,INF=0xffffff;
int n,m;
int U[N],D[N],L[N],R[N];
int row[N],col[N];
int S[N],H[N],ans[N];
int siz;
int node[66][66],mp[66][66];
int st[66],et[66],a,d,num,np[6][6];
int ans1[N],ans2[N],nps[N],npe[N];

/*
int mas[4444][4444];
void print()
{
    memset(mas,0,sizeof(mas));
    for(int i=m+1;i<siz;i++)
    {
        mas[row[i]][col[i]]=1;
    }
    for(int i=0;i<n;i++)
    {
        if(i%num==0) cout<<endl;
        cout<<i%num<<": \t";
        for(int j=1;j<=m;j++)
        {
            cout<<mas[i][j];
            if(j%d==a) cout<<" ";
        }
        cout<<endl;
    }
    cout<<endl;
}
*/
void init()
{
    for(int i=0;i<=m;i++)
    {
        L[i]=i-1;
        R[i]=i+1;
        col[i]=i;
        U[i]=D[i]=i;
        row[i]=-1;
    }
    R[m]=0;
    L[0]=m;
    siz=m+1;
    memset(H,0,sizeof(H));
    memset(S,0,sizeof(S));
    S[0]=INF+1;
}

void insert(int r,int c)
{
    U[siz]=U[c];
    D[siz]=c;
    U[D[siz]]=siz;
    D[U[siz]]=siz;
    if(H[r])
    {
        L[siz]=L[H[r]];
        R[siz]=H[r];
        L[R[siz]]=siz;
        R[L[siz]]=siz;
    }
    else H[r]=L[siz]=R[siz]=siz;
    row[siz]=r;
    col[siz]=c;
    S[c]++;
    siz++;
}

void build()
{
    init();
    for(int i=0;i<a;i++)
    {
        int s,e,j;
        insert(i*num,i+1);
        for(s=st[i];s<=et[i];s++)
        {
            for(e=s;e<=et[i];e++)
            {
                for(j=s;j<=e;j++)
                {
                    insert(i*num+np[s][e],a+i*d+j+1);
                    for(int k=1;k<node[i][0];k++) insert(i*num+np[s][e],a+node[i][k]*d+j+1);
                }
                insert(i*num+np[s][e],i+1);
            }
        }
    }
}

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 resume(int c)
{
    for(int i=U[c];i!=c;i=U[i])
    {
        for(int j=L[i];j!=i;j=L[j])
        {
            D[U[j]]=j;
            U[D[j]]=j;
            S[col[j]]++;
        }
    }
    R[L[c]]=c;
    L[R[c]]=c;
}

bool dfs(int k)
{
    if(R[0]==0) return true;
    int mins=INF,c=0;
    for(int t=R[0];t!=0;t=R[t])
    {
        if(S[t]<mins)
        {
            mins=S[t];
            c=t;
        }
    }
    if(c==0) return false;
    remove(c);
    for(int i=D[c];i!=c;i=D[i])
    {
        ans[k]=row[i];
        for(int j=R[i];j!=i;j=R[j]) remove(col[j]);
        if(dfs(k+1)) return true;
        for(int j=L[i];j!=i;j=L[j]) resume(col[j]);
    }
    resume(c);
    return false;
}

int main()
{
    int b;
    ios::sync_with_stdio(false);
    while(scanf("%d %d %d",&a,&b,&d)==3)
    {
        int i,j,k;
        num=d*(d+1)/2+1;
        n=a*num;
        m=a*d+a;
        for(k=1,i=0;i<d;i++) for(j=i;j<d;j++) np[i][j]=k++;
        memset(mp,0,sizeof(mp));
        for(i=0;i<b;i++)
        {
            scanf("%d %d",&j,&k);
            j--;k--;
            if(j<k) swap(j,k);
            mp[j][k]=1;
        }
        for(i=0;i<a;i++) node[i][0]=1;
        for(j=0;j<a;j++) for(k=0;k<a;k++) if(mp[j][k])
        {
            node[j][node[j][0]++]=k;
            node[k][node[k][0]++]=j;
        }
        for(i=0;i<a;i++)
        {
            scanf("%d %d",&st[i],&et[i]);
            st[i]--;et[i]--;
        }
        build();
        //print();
        if(!dfs(0))
        {
            puts("No solution\n");
            continue;
        }
        for(i=0;i<a;i++)
        {
            j=ans[i]%num;
            if(j){for(int s=0;s<d;s++) for(int e=s;e<d;e++) if(np[s][e]==j){ans1[ans[i]/num]=s+1;ans2[ans[i]/num]=e+1;}}
            else{ans1[ans[i]/num]=ans2[ans[i]/num]=0;}
        }
        for(i=0;i<a;i++) printf("%d %d\n",ans1[i],ans2[i]);
        puts("");
    }
    return 0;
}

抱歉!评论已关闭.