首页 > 代码库 > HDU3231拓扑排序

HDU3231拓扑排序

Box Relations

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1334    Accepted Submission(s): 540
Special Judge


Problem Description
There are n boxes C1, C2, ..., Cn in 3D space. The edges of the boxes are parallel to the x, y or z-axis. We provide some relations of the boxes, and your task is to construct a set of boxes satisfying all these relations.

There are four kinds of relations (1 <= i,j <= ni is different from j):
    • I i j: The intersection volume of Ci and Cj is positive.

    • X i j: The intersection volume is zero, and any point inside Ci has smaller x-coordinate than any point inside Cj.

    • Y i j: The intersection volume is zero, and any point inside Ci has smaller y-coordinate than any point inside Cj.

  • Z i j: The intersection volume is zero, and any point inside Ci has smaller z-coordinate than any point inside Cj.
.
 

 

Input
There will be at most 30 test cases. Each case begins with a line containing two integers n (1 <= n <= 1,000) and R (0 <= R <= 100,000), the number of boxes and the number of relations. Each of the following R lines describes a relation, written in the format above. The last test case is followed by n=R=0, which should not be processed.
 

 

Output
For each test case, print the case number and either the word POSSIBLE or IMPOSSIBLE. If it‘s possible to construct the set of boxes, the i-th line of the following nlines contains six integers x1, y1, z1, x2, y2, z2, that means the i-th box is the set of points (x,y,z) satisfying x1 <= x <= x2, y1 <= y <= y2, z1 <= z <= z2. The absolute values of x1, y1, z1, x2, y2, z2 should not exceed 1,000,000.

Print a blank line after the output of each test case.
 

 

Sample Input
3 2
I 1 2
X 2 3
3 3
Z 1 2
Z 2 3
Z 3 1
1 0
0 0
 

 

Sample Output
Case 1: POSSIBLE
0 0 0 2 2 2
1 1 1 3 3 3
8 8 8 9 9 9
Case 2: IMPOSSIBLE
Case 3: POSSIBLE
0 0 0 1 1 1
 

 

Source
2009 Asia Wuhan Regional Contest Hosted by Wuhan University
 题意:
给出n个立方体之间的位置关系,I a,b 表示a和b相交,X a,b 表示a的x坐标都小于b的x坐标,输出符合条件的立方体的坐标范围
代码:
//把立方体的六个面看成6个点,每个立方体都有自己的约束条件(x1<x2,y1<y2,z1<z2),立方体之间又有
//约束条件,这样三维坐标分成三部分建图,拓扑排序,排在后面的比排在前面的多加1单位长度.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int maxn=2005;
int n,m,in[3][maxn],val[3][maxn];
vector<int>g[3][maxn];
void init()
{
    for(int i=0;i<3;i++){
        for(int j=1;j<=n*2;j++){
            in[i][j]=val[i][j]=0;
            g[i][j].clear();
        }
    }
    for(int i=0;i<3;i++){
        for(int j=1;j<=n;j++){
            in[i][j+n]++;
            g[i][j].push_back(j+n);
        }
    }
}
bool topo()
{
    for(int i=0;i<3;i++){
        queue<int>q;
        int cnt=0;
        for(int j=1;j<=n;j++)
        if(in[i][j]==0){
            val[i][j]=cnt++;
            q.push(j);
        }
        while(!q.empty()){
            int a=q.front();q.pop();
            for(int j=0;j<(int)g[i][a].size();j++){
                int b=g[i][a][j];
                val[i][b]=max(val[i][b],val[i][a]+1);//!
                if(--in[i][b]==0){
                    q.push(b);cnt++;
                }
            }
        }
        if(cnt!=n*2) return 0;
    }
    return 1;
}
int main()
{
    int cas=0;
    while(scanf("%d%d",&n,&m)&&(n+m)){
        init();
        char ch[5];int a,b;
        while(m--){
            scanf("%s%d%d",ch,&a,&b);
            if(ch[0]==I){
                for(int i=0;i<3;i++){
                    in[i][a+n]++;g[i][b].push_back(a+n);
                    in[i][b+n]++;g[i][a].push_back(b+n);
                }
            }
            else if(ch[0]==X){
                in[0][b]++;g[0][a+n].push_back(b);
            }
            else if(ch[0]==Y){
                in[1][b]++;g[1][a+n].push_back(b);
            }
            else{
                in[2][b]++;g[2][a+n].push_back(b);
            }
        }
        printf("Case %d: ",++cas);
        if(topo()){
            printf("POSSIBLE\n");
            for(int i=1;i<=n;i++)
                printf("%d %d %d %d %d %d\n",val[0][i],val[1][i],val[2][i],val[0][i+n],val[1][i+n],val[2][i+n]);
        }
        else printf("IMPOSSIBLE\n");
        printf("\n");
    }
    return 0;
}

 

 

HDU3231拓扑排序