HDU 2647 Reward(拓扑排序,vector实现邻接表)
Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13509 Accepted Submission(s): 4314
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.
then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.
1 2
2 2
1 2
2 1
-1
1 2
2 3
4 5
2 3
4 5
答案:4444
5 4
1 2
2 5
2 4
4 3
答案:4446
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
} #define max_v 10005
int indgree[max_v];
struct node
{
int index,v;
node(int x,int y)
{
index=x;
v=y;
}
};
vector <int> vv[max_v];
int n,m;
LL sum;
queue<node> q;
int tpsort()
{
sum=;
while(!q.empty())
q.pop();
for(int i=;i<=n;i++)
{
if(indgree[i]==)
q.push(node(i,));
} int c=;
int flag=;
int p1,p2;
while(!q.empty())
{
if(q.size()>)
flag=;
p1=q.front().index;
p2=q.front().v;
q.pop();
c++;
sum+=p2;
for(int i=;i<vv[p1].size();i++)
{
indgree[vv[p1][i]]--;
if(indgree[vv[p1][i]]==)
q.push(node(vv[p1][i],p2+));
}
}
/*
拓扑完之后,存在没有入队的点,那么该点就一定是环上的
*/
if(c!=n)//存在环
return ;
else if(flag)//能拓扑但存在多条路
return ;
return -;//能拓扑且存在唯一拓扑路径
}
int main()
{
int x,y;
while(~scanf("%d %d",&n,&m))
{
memset(indgree,,sizeof(indgree));
for(int i=;i<=n;i++)
{
vv[i].clear();
}
int flag=-;
for(int i=;i<m;i++)
{
scanf("%d %d",&y,&x);
if(count(vv[x].begin(),vv[x].end(),y)==)//防止重边
{
vv[x].push_back(y);
indgree[y]++;
}
if(count(vv[y].begin(),vv[y].end(),x)!=)//环的一种
{
flag=;
}
}
flag=tpsort();
if(flag==)
{
printf("-1\n");
}else
{
printf("%lld\n",sum);
}
}
}
HDU 2647 Reward(拓扑排序,vector实现邻接表)的更多相关文章
- HDU.2647 Reward(拓扑排序 TopSort)
HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...
- ACM: hdu 2647 Reward -拓扑排序
hdu 2647 Reward Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Des ...
- HDU 2647 Reward (拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...
- hdu 2647 Reward(拓扑排序+优先队列)
Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...
- hdu 2647 Reward(拓扑排序+反图)
题目链接:https://vjudge.net/contest/218427#problem/C 题目大意: 老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平 ...
- HDU 2647 逆向拓扑排序
令每一个员工都有一个自己的等级level[i] , 员工等级越高,那么工资越高,为了使发的钱尽可能少,所以每一级只增加一单位的钱 输入a b表示a等级高于b,那么我们反向添加边,令b—>a那么i ...
- HDU 1285--确定比赛名次【拓扑排序 && 邻接表实现】
确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 题解报告:hdu 2647 Reward(拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...
- STL中的vector实现邻接表
/* STL中的vector实现邻接表 2014-4-2 08:28:45 */ #include <iostream> #include <vector> #include ...
- 利用C++ STL的vector模拟邻接表的代码
关于vector的介绍请看 https://www.cnblogs.com/zsq1993/p/5929806.html https://zh.cppreference.com/w/cpp/conta ...
随机推荐
- BOM(JavaScript高程笔记)
再次编辑于20160115 一.window对象 双重角色 JS访问浏览器窗口的接口 ECAMAscript规定的Global对象 1.全局作用域 所有在全局作用域中声明的变量.函数都会变成windo ...
- 04-基本的mysql语句
[转]04-基本的mysql语句 本节课先对mysql的基本语法初体验. 操作文件夹(库) 增 create database db1 charset utf8; 查 # 查看当前创建的数据库 sho ...
- python生成器简单了解
1.什么是生成器 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素, ...
- 设计模式原则(1)--Single Responsibility Principle(SRP)--单一职责原则
1.定义: 不要存在多于一个导致类变更的原因.通俗的说,即一个类只负责一项职责. 2.使用场景: 如果类A有两个职责:d1,d2.当职责d1需要修改时,可能会导致原本运行正常的职责d2功能产生问题. ...
- crontab -让服务器执行定时任务
1.启动服务 一般启动服务用 "/sbin/service crond start"就可以了,如果是root用户就是 "sudo service crond start& ...
- js-权威指南学习笔记19.2
1.jQuery动画是异步的,会立刻返回,但动画会在后台执行,可传入函数作为动画完成的回调函数. 2.jQuery动画默认是队列化的. 3.stop()方法接受两个可选的布尔值参数,如果第一个参数是t ...
- python邮件发送
'''qq邮件与其他邮件有所不同,下以我的qq邮件为例(切勿转载):''' import osimport smtplibfrom email.mime.text import MIMEText # ...
- tinyint、smallint、bigint、int 区别
1byte=8bit [tinyint] 从 0 到 255 的整型数据.存储大小为 1 字节.如果设置为UNSIGNED类型,只能存储从0到255的整数,不能用来储存负数. [smallint] ...
- element-ui table
额,最近有点频繁记录.感觉遇到了很多的问题.然后不断的查,不断的尝试修改.然后得到解决.还是记录关于element-ui的使用.这次是关于表格的使用. 表格使用其实平时用的话也就那么几个属性.虽然官网 ...
- 快速配置webpack 4.x
区分开发模式和生产模式: npm run start——开发模式,启用devServer,文件的改动实时更新.刷新 npm run build——生产模式,打包文件到dist文件夹 // packag ...