Power Stations HDU - 3663

Posted wtsruvf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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): 748
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
 
解析:
  选了某个村庄后 连带着和当前村庄相连的村庄也会被选择  这是不是就成为了一个约束条件所以把每个村庄安排在一行 每个村庄都有d个单位长度
把每个村庄的发电机的工作时间区间 的所有可能情况枚举出来 每种可能占一行
每个村庄再增加一行 且每行再增加n列 用于标记当前村庄是否被选择 且 只选择一次
 
这个T了
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 510000, INF = 0x7fffffff;

int S[2000], head[2000], vis[2000];
int U[maxn], D[maxn], L[maxn], R[maxn];
int C[maxn], X[maxn];
int n, m, ans, ret, d;

void init()
{
    for(int i = 0; i <= m; i++)
        D[i] = i, U[i] = i, R[i] = i + 1, L[i] = i - 1;
    L[0] = m, R[m] = 0;
    mem(S, 0), mem(head, -1);
    ans = m + 1;
}

void delc(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[C[j]]--;

}

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

void add(int r, int c)
{
    ans++, S[c]++, C[ans] = c, X[ans] = r;
    D[ans] = D[c];
    U[ans] = c;
    U[D[c]] = ans;
    D[c] = ans;
    if(head[r] < 0) head[r] = L[ans] = R[ans] = ans;
    else L[ans] = head[r], R[ans] = R[head[r]],L[R[head[r]]] = ans, R[head[r]] = ans;
}


bool dfs(int sh)
{
    if(!R[0])
    {
        ret = sh;
        return true;
    }
    int c = R[0];
    delc(c);
    for(int i = D[c]; i != c; i = D[i])
    {
        vis[sh] = X[i];
        for(int j = R[i]; j != i; j = R[j])
            delc(C[j]);
        if(dfs(sh + 1)) return true;
        for(int j = L[i]; j != i; j = L[j])
            resc(C[j]);
    }
    resc(c);
    return false;
}

int g[65][65];
struct node
{
    int s, t, id;
}Node[2000], tmp[2000];


int main()
{

    int u, v, s, t;
    while(~scanf("%d%d%d", &n, &m, &d))
    {
        mem(g, 0);
        for(int i = 0; i < m; i++)
        {
            rd(u), rd(v);
            g[u][v] = g[v][u] = 1;
        }
        m = n * d + n;
        init();
        int cnt = 0;
        rap(i, 1, n)
        {
            g[i][i] = 1;
            rd(Node[i].s), rd(Node[i].t);
            ++cnt;
            add(cnt, n * d + i);
            tmp[cnt].s = 0, tmp[cnt].t = 0, tmp[cnt].id = i;
            rap(j, Node[i].s, Node[i].t)
            {
                rap(k, j, Node[i].t)
                {
                    ++cnt;
                    add(cnt, n * d + i);
                    tmp[cnt].s = j, tmp[cnt].t = k, tmp[cnt].id = i;
                    rap(a, 1, n)
                        if(g[i][a])
                            rap(b, j, k)
                                add(cnt, (a - 1) * d + b);
                }
            }

        }

        if(!dfs(0))
        {
            printf("No solution\n");
        }
        else
        {
            mem(X, 0), mem(C, 0);
            rep(i, 0, ret)
            {
                X[tmp[vis[i]].id] = tmp[vis[i]].s;
                C[tmp[vis[i]].id] = tmp[vis[i]].t;
            }
            rap(i, 1, n)
                printf("%d %d\n", X[i], C[i]);
        }
        printf("\n");
    }

    return 0;
}

 

  
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define REP( i , a , b ) for ( int i = a ; i < b ; ++ i )
#define REV( i , a , b ) for ( int i = a - 1 ; i >= b ; -- i )
#define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define FOV( i , a , b ) for ( int i = a ; i >= b ; -- i )
#define REC( i , A , o ) for ( int i = A[o] ; i != o ; i = A[i] )
#define CLR( a , x ) memset ( a , x , sizeof a )

const int MAXN = 65 ;
const int MAXM = 2000 ;
const int MAXNODE = 500000 ;

struct Node {
    int l , r , idx ;
    Node () {}
    Node ( int l , int r , int idx ) : l ( l ) , r ( r ) , idx ( idx ) {}
} ;

struct DLX {
    int U[MAXNODE] , D[MAXNODE] , L[MAXNODE] , R[MAXNODE] ;
    int row[MAXNODE] , col[MAXNODE] ;
    int S[MAXM] , H[MAXM] ;
    int deep , ans[MAXN] ;
    int n , m ;
    int size ;


    int N , M , DD ;
    int X[MAXN] , Y[MAXN] ;
    Node node[MAXM] ;
    int G[MAXN][MAXN] ;

    void remove ( int c ) {
        L[R[c]] = L[c] ;
        R[L[c]] = R[c] ;
        REC ( i , D , c )
            REC ( j , R , i ) {
                D[U[j]] = D[j] ;
                U[D[j]] = U[j] ;
                -- S[col[j]] ;
            }
    }

    void resume ( int c ) {
        REC ( i , U , c )
            REC ( j , L , i ) {
                ++ S[col[j]] ;
                U[D[j]] = j ;
                D[U[j]] = j ;
            }
        R[L[c]] = c ;
        L[R[c]] = c ;
    }

    int dance ( int d ) {
        if ( R[0] == 0 ) {
            deep = d ;
            return 1 ;
        }
        int c = R[0] ;
        REC ( i , R , 0 )
            if ( S[c] > S[i] )
                c = i ;
        //printf ( "ok\n" ) ;
        remove ( c ) ;
        REC ( i , D , c ) {
            ans[d] = row[i] ;
            REC ( j , R , i )
                remove ( col[j] ) ;
            if ( dance ( d + 1 ) )
                return 1 ;
            REC ( j , L , i )
                resume ( col[j] ) ;
        }
        resume ( c ) ;
        return 0 ;
    }

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

    void init () {
        CLR ( H , -1 ) ;
        FOR ( i , 0 , n ) {
            S[i] = 0 ;
            L[i] = i - 1 ;
            R[i] = i + 1 ;
            U[i] = i ;
            D[i] = i ;
        }
        L[0] = n ;
        R[n] = 0 ;
        size = n ;
    }

    void solve () {
        int x , y ;
        n = N * DD + N ;
        m = 0 ;
        init () ;
        CLR ( G , 0 ) ;
        CLR ( node , 0 ) ;
        REP ( i , 0 , M ) {
            scanf ( "%d%d" , &x , &y ) ;
            G[x][y] = G[y][x] = 1 ;
        }
        FOR ( i , 1 , N )
            G[i][i] = 1 ;
        FOR ( idx , 1 , N ) {
            scanf ( "%d%d" , &x , &y ) ;
            int tmp = N * DD + idx ;
            ++ m ;
            link ( m , tmp ) ;//none select
            node[m] = Node ( 0 , 0 , idx ) ;
            FOR ( i , x , y )
                FOR ( j , i , y ) {
                    ++ m ;
                    link ( m ,  tmp ) ;
                    node[m] = Node ( i , j , idx ) ;
                    FOR ( a , 1 , N )
                        if ( G[idx][a] )
                            FOR ( b , i , j )
                                link ( m , ( a - 1 ) * DD + b ) ;
                }
        }
        if ( !dance ( 0 ) )
            printf ( "No solution\n" ) ;
        else {
            CLR ( X , 0 ) ;
            CLR ( Y , 0 ) ;
            REP ( i , 0 , deep ) {
                X[node[ans[i]].idx] = node[ans[i]].l ;
                Y[node[ans[i]].idx] = node[ans[i]].r ;
            }
            FOR ( i , 1 , N )
                printf ( "%d %d\n" , X[i] , Y[i] ) ;
        }
        printf ( "\n" ) ;
    }

} dlx ;

int main () {
    while ( ~scanf ( "%d%d%d" , &dlx.N , &dlx.M , &dlx.DD  ) )
        dlx.solve () ;
    return 0 ;
}

 

Power Stations

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

以上是关于Power Stations HDU - 3663的主要内容,如果未能解决你的问题,请参考以下文章

搜索(DLX):HDU 3663 Power Stations

HDU 3663 舞蹈链之不可重复覆盖

HDU 2485 Destroying the bus stations(费用流)

hdu 6034 Balala Power!

HDU 6034 Balala Power!(贪心 + 进制)

HDU 6034 Balala Power!(贪心+排序)