Reward

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

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
 
Recommend
yifenfei   |   We have carefully selected several similar problems for you:  3342 1811 2680 2112 2729 
 
分析:
a b:表示a的钱应该比b多,基本工资是888,所以a最少是889
坑点:
1.必须使用邻接表存图,邻接矩阵会超内存
2.可能存在多条拓扑路径,所以不能最后直接算总工资(有点没有说明白,就是有的人工资可以和其他人一样,看下面的两组数据就知道了)
数据:
5 5
1 2
2 3
4 5
2 3
4 5

答案:4444

5 4
1 2
2 5
2 4
4 3

答案:4446

 
所以我们队列里面得存结构体!!!
code:
#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实现邻接表)的更多相关文章

  1. HDU.2647 Reward(拓扑排序 TopSort)

    HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...

  2. ACM: hdu 2647 Reward -拓扑排序

    hdu 2647 Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Des ...

  3. HDU 2647 Reward (拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...

  4. hdu 2647 Reward(拓扑排序+优先队列)

    Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...

  5. hdu 2647 Reward(拓扑排序+反图)

    题目链接:https://vjudge.net/contest/218427#problem/C 题目大意: 老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平 ...

  6. HDU 2647 逆向拓扑排序

    令每一个员工都有一个自己的等级level[i] , 员工等级越高,那么工资越高,为了使发的钱尽可能少,所以每一级只增加一单位的钱 输入a b表示a等级高于b,那么我们反向添加边,令b—>a那么i ...

  7. HDU 1285--确定比赛名次【拓扑排序 &amp;&amp; 邻接表实现】

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  8. 题解报告:hdu 2647 Reward(拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...

  9. STL中的vector实现邻接表

    /* STL中的vector实现邻接表 2014-4-2 08:28:45 */ #include <iostream> #include <vector> #include  ...

  10. 利用C++ STL的vector模拟邻接表的代码

    关于vector的介绍请看 https://www.cnblogs.com/zsq1993/p/5929806.html https://zh.cppreference.com/w/cpp/conta ...

随机推荐

  1. BZOJ4373: 算术天才⑨与等差数列(线段树 hash?)

    题意 题目链接 Sol 正经做法不会,听lxl讲了一种很神奇的方法 我们考虑如果满足条件,那么需要具备什么条件 设mx为询问区间最大值,mn为询问区间最小值 mx - mn = (r - l) * k ...

  2. js-ES6学习笔记-module(3)

    1.如果想设置跨模块的常量(即跨多个文件),或者说一个值要被多个模块共享,可以采用下面的写法. // constants.js 模块 export const A = 1; export const ...

  3. js-ES6学习笔记-正则的扩展

    1.在ES5中,RegExp构造函数的参数有两种情况.第一种情况是,参数是字符串,这时第二个参数表示正则表达式的修饰符(flag).第二种情况是,参数是一个正则表示式,这时会返回一个原有正则表达式的拷 ...

  4. FineReport8.0如何连接FineIndex取数分析

    1. 描述 在3.7及之前版本,FineReport连接都是通过安装多维数据集插件,然后通过多维数据库的方式连接FineBI(3.4-3.6对应711,3.7对应8.0),从4.0版本开始,FineR ...

  5. paste 命令

    Linux paste命令用于合并文件的列. paste指令会把每个文件以列对列的方式,一列列地加以合并. 语法: paste [-s][-d <间隔字符>][--help][--vers ...

  6. AsyncTask GET请求

    布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= ...

  7. 记一次寻找appbug的问题

    公司规模 3000人以上 全国500强. 从总部刚交接过来的代码. 1.找不到代码.代码部分丢失.(由于没有交接,没有任何相关文档,花了一天确定代码丢失.从总部找到部分代码) 2.查找测试库,发现测试 ...

  8. [原创]RedHat 安装MySQL数据库

    朋友购买了阿里云的服务器,服务器上自带有CentOS操作系统,但是开发软件需要自己安装,接下来将介绍本地RedHat Linux 5.10虚拟机上搭建Mysql数据库. 一.软件准备 (1)jdk-6 ...

  9. 学习ASP.NET之旅

    1.HTML 2.DIV 3.CSS 4.JS脚本 5.javascript 6.J-Query 7.AJAX 8.c# 9.IIS配置 10.ASP.NET怎样添加控件,与winform的控件添加方 ...

  10. 全球首款完全开源的堡垒机,符合 4A 的专业运维审计系统Jumpserver

    Jumpserver是全球首款完全开源的堡垒机,是符合 4A 的专业运维审计系统. http://www.jumpserver.org https://github.com/jumpserver/ju ...