拓扑排序!

在这里插入图片描述

HDU - 2647 Reward

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

题意:
给定数据n和m,n是工人数,m是行数,m行每行有两个数据a、b,要求a的奖金要大于b的奖金,最低奖金是888,求老板发给工人的最少奖金数。

#include<iostream>
#include<queue>
#include<vector>
#include<memory.h>
using namespace std;

const int N = 1e4+5;
vector<int> v[N];	
//邻接表,每个节点保存与它相连的边的另一个端点 

queue<int> q;
int n, m, u, r, ind[N], cnt[N];
//ind记录每个节点的入度
//cnt记录每个节点该得到的奖励

bool topsort()
{
	int num = 0;
	for(int i = 1; i <= n; ++i)
		if(!ind[i])
			q.push(i);
	while(!q.empty())
	{
		int now = q.front();
		q.pop();
		num++;
		for(unsigned int i = 0; i < v[now].size(); ++i)
		{
			if(--ind[v[now][i]] == 0)
				q.push(v[now][i]);
			cnt[v[now][i]] = cnt[now]+1;
		}
	}
	if(num == n)
		return 1;
	else
		return 0;
 } 
 
 int main()
 {
 	while(cin >> n >> m)
 	{
 		for(int i = 1; i <= n; ++i)
 			v[i].clear();
 		memset(ind, 0, sizeof(ind));
 		memset(cnt, 0, sizeof(cnt));
 		while(m--)
 		{
 			cin >> u >> r;
 			v[r].push_back(u);
 			ind[u]++;
		 }
		 if(topsort())
		 {
		 	int sum = 0;
		 	for(int i = 1; i <= n; ++i)
		 		sum += cnt[i];
		 	cout << (sum + 888*n) << endl;
		 }
		 else
		 	cout << "-1" << endl;
	 }
	 return 0;
 }

HDU2094 产生冠军

有一群人,打乒乓球比赛,两两捉对撕杀,每两个人之间最多打一场比赛。
球赛的规则如下:
如果A打败了B,B又打败了C,而A与C之间没有进行过比赛,那么就认定,A一定能打败C。
如果A打败了B,B又打败了C,而且,C又打败了A,那么A、B、C三者都不可能成为冠军。
根据这个规则,无需循环较量,或许就能确定冠军。你的任务就是面对一群比赛选手,在经过了若干场撕杀之后,确定是否已经实际上产生了冠军。
Input
输入含有一些选手群,每群选手都以一个整数n(n<1000)开头,后跟n对选手的比赛结果,比赛结果以一对选手名字(中间隔一空格)表示,前者战胜后者。如果n为0,则表示输入结束。

Output
对于每个选手群,若你判断出产生了冠军,则在一行中输出“Yes”,否则在一行中输出“No”。

Sample Input
3
Alice Bob
Smith John
Alice Smith
5
a c
c d
d e
b e
a d
0
Sample Output
Yes
No

#include<iostream>
#include<string>
#include<map>
#include<set>
using namespace std;

map<string, map<string, int>>G;
map<string, int>vis;
set<string>name;
int n;

void toposort()
{
	set<string>::iterator it;
	int count = 0;
	for(it = name.begin(); it != name.end(); ++it)
		if(vis[*it] == 0)
			count++;
	if(count == 1)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}

int main()
{
	while(cin >> n && n)
	{
		string a, b;
		G.clear();
		vis.clear();
		name.clear();
		for(int i = 0; i < n; ++i)
		{
			cin >> a >> b;
			name.insert(a);
			name.insert(b);
			if(!G[a][b])
			{
				G[a][b]++;
				vis[b]++;
			}
		}
		toposort();
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值