hdoj 2647 Reward【反向拓扑排序】
Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5617 Accepted Submission(s):
1707
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.
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.
uncle needs to distribute .If it's impossible to fulfill all the works' demands
,print -1.
#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#define MAX 20010
using namespace std;
vector<int>map[MAX];
int vis[MAX];
int reward[MAX];
int n,m,sum;
void getmap()
{
int i,j;
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
map[i].clear();
for(i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
map[b].push_back(a);
vis[a]++;
}
}
void tuopu()
{
int i,j,ok=0,sum=0;
queue<int>q;
memset(reward,0,sizeof(reward));
while(!q.empty())
q.pop();
for(i=1;i<=n;i++)
if(vis[i]==0)
{
q.push(i);
reward[i]=888;
}
int u,v;
int ans=0;
while(!q.empty())
{
u=q.front();
q.pop();
ans++;
for(i=0;i<map[u].size();i++)
{
v=map[u][i];
vis[v]--;
if(vis[v]==0)
{
q.push(v);
reward[v]=reward[u]+1;
}
}
}
if(ans!=n)
printf("-1\n");
else
{
for(i=1;i<=n;i++)
sum+=reward[i];
printf("%d\n",sum);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
getmap();
tuopu();
}
return 0;
}
hdoj 2647 Reward【反向拓扑排序】的更多相关文章
- 题解报告:hdu 2647 Reward(拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...
- HDU 2647 Reward(拓扑排序+判断环+分层)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...
- HDU 2647 Reward 【拓扑排序反向建图+队列】
题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...
- HDU 2647 Reward(拓扑排序,vector实现邻接表)
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 杭电 2647 Reward (拓扑排序反着排)
Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to ...
- hdu 2647 Reward(拓扑排序,反着来)
Reward Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submis ...
- CF-825E Minimal Labels 反向拓扑排序
http://codeforces.com/contest/825/problem/E 一道裸的拓扑排序题.为什么需要反向拓扑排序呢?因为一条大下标指向小下标的边可能会导致小下标更晚分配到号码,导致字 ...
- 逃生(HDU4857 + 反向拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 题面是中文题面,就不解释题意了,自己点击链接去看下啦~这题排序有两个条件,一个是按给定的那个序列 ...
- HDU-4857-逃生-反向拓扑排序+优先队列
HDU-4857 题意就是做一个符合条件的排序,用到拓扑序列. 我一开始wa了多发,才发现有几个样例过不了,发现1->2->3...的顺序无法保证. 后来就想用并查集强连,还是wa: 后来 ...
随机推荐
- 使用APPLICATION制作缓存,转存一下,有一段写的还可以。
public sealed class CacheManager { private HttpApplicationState appPool = null; /// <summar ...
- JQuery slideToggle闪烁问题及解决办法
在使用slideToggle的时候,会出现在实现隐藏效果后闪烁一下在消失,找了很多原因,本以为是浏览器问题,后来发现是文档定义类型的问题... 原来页面的文档定义:<!DOCTYPE HTML ...
- IE6 for WIN8
分享一款微软原生内核的Ie6,绿色免安装,无广告,无插件. 点击下载IE6 for WIN8 来源:子涵的博客
- 通过try、except和else的使用来使Python程序更加“强壮”
在执行的程序中,难免会碰到因为一些原因如输入输出导致致命性错误产生的情况(如因为输入的文件名错误而导致无法运行相关的代码.).此时你不希望程序直接挂掉,而是通过显示一些信息,使其平稳的结束.此时,就可 ...
- css的继承、层叠和特殊性
1,继承 css的某些样式是具有继承性的,那么什么是继承呢?继承是一种规则,它允许样式不仅应用于某个特定html标签元素,而且应用于其后代. 但注意有一些css样式是不具有继承性的.如border: ...
- init_sequence所对应的函数
一.init_sequence内容 init_fnc_t *init_sequence[] = { cpu_init, /* basic cpu dependent setup */ board_in ...
- Unity干中学——如何实现游戏截图?
using UnityEngine; using System.Collections; using System.IO; public class ScreenShot : MonoBehaviou ...
- MFC学习指南大纲
最近一直在做MFC的项目,顺便学习一下以下MFC核心知识吧: 大纲: 1. 消息队列 2. send message 3. post message 4. 面向对象编程 5. 指针 一个一个来学习哈.
- websphere性能设置和日常维护
一. 确认磁盘空间是否满足要求1. WebSphere 应用服务器自身代码的占用空间.这个空间一般在1G左右,在不同的系统平台上略有差异. 2. 概要文件所占的空间.WebSphere应用服务器V6. ...
- iOS9 App Thinning(应用瘦身)功能介绍
iOS9 发布后,产生了一个使 App Thinning 无法正常运行的 bug.在iOS9.0.2 版本中,这个 bug 已经被修复,App Thinning 已经可以正常使用.当你从应用商店(Ap ...