POJ 3687:Labeling Balls(优先队列+拓扑排序)

id=3687">Labeling Balls

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10178 Accepted: 2815

Description

Windy has N balls of distinct weights from 1 unit toN units. Now he tries to label them with 1 toN in such a way that:

  1. No two balls share the same label.
  2. The labeling satisfies several constrains like "The ball labeled witha is lighter than the one labeled withb".

Can you help windy to find a solution?

Input

The first line of input is the number of test case. The first line of each test case contains two integers,N (1 ≤N ≤ 200) and
M (0 ≤ M ≤ 40,000). The nextM line each contain two integersa and
b indicating the ball labeled witha must be lighter than the one labeled withb. (1 ≤
a, bN) There is a blank line before each test case.

Output

For each test case output on a single line the balls‘ weights from label 1 to labelN. If several solutions exist, you should output the one with the smallest weight for label 1, then with the smallest weight for label
2, then with the smallest weight for label 3 and so on... If no solution exists, output -1 instead.

Sample Input

5

4 0

4 1
1 1

4 2
1 2
2 1

4 1
2 1

4 1
3 2

Sample Output

1 2 3 4
-1
-1
2 1 3 4
1 3 2 4

在主要的拓扑排序的基础上又添加了一个要求:编号最小的节点要尽量排在前面;在满足上一个条件的基础上,编号第二小的节点要尽量排在前面;

在满足前两个条件的基础上。编号第三小的节点要尽量排在前面……依此类推。点击打开链接又是看结题报告。。。

。哎。。

。。

太弱了

第一百篇。。留念。。。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>

using namespace std;

const int M = 250 ;
int t, n, m;
int outint[M];
int out[M];
int in[M];
int cut;
vector<int>amap[M];
int flag;

bool toposort()
{
    cut = 0;
    priority_queue<int>que;
    for(int i=1; i<=n; i++)
        if( !in[i] )
        que.push(i);
    while( !que.empty() )
    {
        int u = que.top();
        que.pop();
        outint[ cut++] = u;
        for( int i=0; i<amap[u].size(); i++ )
        {
            int v = amap[u][i];
            if( --in[v]==0 )
                que.push(v);
        }
    }
    if( cut<n )
        return false;
    else
        return true;
}

int main()
{
    scanf( "%d", &t );
    while( t-- )
    {
        memset( in, 0, sizeof(in) );
        scanf( "%d%d", &n, &m );
        for( int i=1; i<=n; i++ )
            amap[i].clear();
        for( int i=1; i<=m; i++ )
        {
            int a, b;
            scanf( "%d%d", &a, &b );
            amap[b].push_back(a);
            in[a]++;
        }
        if( !toposort() )
            printf("-1\n");
        else
        {
            for( int i=0; i<n; i++ )
                out[ outint[i] ] = n-i;
            for(int i=1; i<=n; i++)
            {
                if(i<n)
                    printf( "%d ", out[i] );
                else
                    printf( "%d\n", out[i] );
            }
        }
    }

    return 0;
}
时间: 2024-12-24 08:49:07

POJ 3687:Labeling Balls(优先队列+拓扑排序)的相关文章

POJ 3687 Labeling Balls【拓扑排序 优先队列】

题意:给出n个人,m个轻重关系,求满足给出的轻重关系的并且满足编号小的尽量在前面的序列 因为输入的是a比b重,但是我们要找的是更轻的,所以需要逆向建图 逆向建图参看的这一篇http://blog.csdn.net/scf0920/article/details/28108243 然后用优先队列来实现的参看的这一篇 http://ycool.com/post/u9ahrwg#algo3 1 #include<iostream> 2 #include<cstdio> 3 #includ

POJ 3687 Labeling Balls 逆向拓扑排序

链接: poj3687 题意: 有N个标号为1~N的小球,重量(不包括断言)依次增加 ,现给出M句断言 ,断言格式为a b 表示小球a轻于小球b     要求根据重量大小依次输出1~N号小球应在的的位置(重量递增)不满足断言则输出-1 题解: 因为重量是依次增加的  不能按常规的构造edge[a][b]=1生成拓扑排序 既然关系格式和一般拓扑排序是相反的  我们可以尝试着构造反向边edge[b][a]=1: indegree[a]++;   再根据小球标号从后往前进行拓扑排序,即可得到一幅完整的

poj 3687 Labeling Balls 【拓扑排序】

题目链接:http://poj.org/problem?id=3687 注意重边 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include <string> #include <functional> #include <iterator> #include

[ACM] POJ 3687 Labeling Balls (拓扑排序,逆向建边)

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share

[ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 toN in such a way that: No two balls share

POJ - 3687 Labeling Balls (拓扑排序)

题意:N个点,给出M条两个点u.v,满足u比值小.给这N个点编号,要求排在前的比排在后的质量小,且编号不重复.求每点能得到最小编号的编号方法. 分析:用拓扑排序求解. 用优先队列来存待标记的点,编号大的点优先出队列,然后从大到小依次标记(编号小的优先肯定是错的,当时wa死了). 若求不出拓扑排序则答案无解. #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #i

poj 3687 Labeling Balls(拓补排序)

Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share the same label. The labeling satisfies several constrains like "The ball labeled with a is lighter

POJ3687 Labeling Balls (拓扑排序)经典

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11469   Accepted: 3295 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share

Labeling Balls(拓扑排序wa)

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12466   Accepted: 3576 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share

HDU 3687 Labeling Balls(逆向拓扑)

Labeling Balls Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share the same label. The labeling satisfies several constrains like "The ball labeled wi