程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu1285+hdu2467(拓撲排序)

hdu1285+hdu2467(拓撲排序)

編輯:C++入門知識

確定比賽名次

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10604 Accepted Submission(s): 4150


Problem Description 有N個比賽隊(1<=N<=500),編號依次為1,2,3,。。。。,N進行比賽,比賽結束後,裁判委員會要將所有參賽隊伍從前往後依次排名,但現在裁判委員會不能直接獲得每個隊的比賽成績,只知道每場比賽的結果,即P1贏P2,用P1,P2表示,排名時P1在P2之前。現在請你編程序確定排名。

Input 輸入有若干組,每組中的第一行為二個數N(1<=N<=500),M;其中N表示隊伍的個數,M表示接著有M行的輸入數據。接下來的M行數據中,每行也有兩個整數P1,P2表示即P1隊贏了P2隊。

Output 給出一個符合要求的排名。輸出時隊伍號之間有空格,最後一名後面沒有空格。

其他說明:符合條件的排名可能不是唯一的,此時要求輸出時編號小的隊伍在前;輸入數據保證是正確的,即輸入數據確保一定能有一個符合要求的排名。

Sample Input
4 3
1 2
2 3
4 3

Sample Output
1 2 4 3

Author SmallBeer(CML)
Source 杭電ACM集訓隊訓練賽(VII)
Recommend lcy | We have carefully selected several similar problems for you: 2647 3342 1811 1548 1532

題解:
本題是道比較裸的拓撲排序題,注意重邊就行了,還有本題符合條件的排名可能不是唯一的,此時要求輸出時編號小的隊伍在,其次就是輸出格式的限定!

#include
#include
using namespace std;

const int MAXN=500+10;
int inDev[MAXN];
bool visited[MAXN];
bool g[MAXN][MAXN];
int n,m;

void init()
{
	memset(g,0,sizeof(g));
	memset(visited,0,sizeof(visited));
	memset(inDev,0,sizeof(inDev));
}

void input()
{
	int a,b;
	for(int i=0;i

Reward

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3580 Accepted Submission(s): 1085


Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards.
The workers will compare their rewards ,and some one may have demands of the distributing of rewards ,just like a's reward should more than b's.Dandelion's unclue wants to fulfill all the demands, of course ,he wants to use the least money.Every work's reward will be at least 888 , because it's a lucky number.
Input One line with two integers n and m ,stands for the number of works and the number of demands .(n<=10000,m<=20000)
then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.
Output For every case ,print the least money dandelion 's uncle needs to distribute .If it's impossible to fulfill all the works' demands ,print -1.
Sample Input
2 1
1 2
2 2
1 2
2 1

Sample Output
1777
-1

Author dandelion
Source 曾是驚鴻照影來 題解:
本題同上也是道比較容易看出的拓撲排序題,不同的建立拓撲結構圖是應該逆向見圖,這樣能夠保證前者比後者大。
#include
#include
#include
#include
using namespace std;

const int MAXN=10000+10;
vectorg[MAXN];
int inDev[MAXN];
int money[MAXN];
int n,m;

void init()
{
	for(int i=0;iq;
	int tag=0,ans=0;
	for(int i=1;i<=n;i++)
	{
		if(inDev[i]==0)		
			q.push(i);
	}
	while(!q.empty())
	{
		++tag;
		int now=q.front();
		ans+=money[now];
		q.pop();
		for(int i=0;i

246

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved