Reward

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 16602 Accepted Submission(s): 5308

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

解题思路:

反向建图后拓扑排序,就为了复习下链式前向星和拓扑排序

#include <bits/stdc++.h>
using namespace std;
/* freopen("k.in", "r", stdin);
freopen("k.out", "w", stdout); */
//clock_t c1 = clock();
//std::cerr << "Time:" << clock() - c1 <<"ms" << std::endl;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define de(a) cout << #a << " = " << a << endl
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int, int> VII;
#define inf 0x3f3f3f3f
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll MAXN = 5e5 + 7;
const ll MAXM = 1e6 + 7;
const ll MOD = 998244353;
const double eps = 1e-6;
const double pi = acos(-1.0);
int n, m;
int cnt = -1;
int indegree[MAXN];
int head[MAXN];
int red[MAXN];
struct Edge
{
int u, v, Next;
Edge(int uu = 0, int vv = 0, int NN = 0) { u = uu, v = vv, Next = NN; }
} e[MAXN];
void add(int u, int v)
{
e[++cnt].v = v;
e[cnt].u = u;
e[cnt].Next = head[u];
head[u] = cnt;
}
ll topo()
{
int tot = 0;
queue<int> q;
bool flag1 = false;
for (int i = 1; i <= n; i++)
if (!indegree[i])
q.push(i);
while (!q.empty())
{
int now = q.front();
q.pop();
tot++;
if (tot > n)
break;
for (int i = head[now]; ~i; i = e[i].Next)
{
int v = e[i].v;
indegree[v]--;
if (!indegree[v])
{
q.push(v);
red[v] = red[now] + 1;
}
}
}
if (tot != n)
return -1;
ll ans = n * 888;
for (int i = 1; i <= n; i++)
ans += red[i];
return ans;
}
int main()
{
while (~scanf("%d%d", &n, &m))
{
cnt = -1;
memset(red, 0, sizeof(red));
memset(head, -1, sizeof(head));
memset(indegree, 0, sizeof(indegree));
while (m--)
{
int u, v;
scanf("%d%d", &u, &v);
add(v, u);
indegree[u]++;
}
printf("%lld\n", topo());
}
return 0;
}

HDU-2647 Reward(链式前向星+拓扑排序)的更多相关文章

  1. 洛谷 P1352 没有上司的舞会【树形DP/邻接链表+链式前向星】

    题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri, ...

  2. HDU 2544最短路 【dijkstra 链式前向星+优先队列优化】

    最开始学最短路的时候只会用map二维数组存图,那个时候还不知道这就是矩阵存图,也不懂得效率怎么样 经过几个月的历练再回头看最短路的题, 发现图可以用链式前向星来存, 链式前向星的效率是比较高的.对于查 ...

  3. hdu2647 逆拓扑,链式前向星。

    pid=2647">原文地址 题目分析 题意 老板发工资,可是要保证发的工资数满足每一个人的期望,比方A期望工资大于B,仅仅需比B多1元钱就可以.老板发的最低工资为888元.输出老板最 ...

  4. 链式前向星+SPFA

    今天听说vector不开o2是数组时间复杂度常数的1.5倍,瞬间吓傻.然后就问好的图表达方式,然后看到了链式前向星.于是就写了一段链式前向星+SPFA的,和普通的vector+SPFA的对拍了下,速度 ...

  5. 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板

    一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...

  6. 图的存储结构:邻接矩阵(邻接表)&链式前向星

    [概念]疏松图&稠密图: 疏松图指,点连接的边不多的图,反之(点连接的边多)则为稠密图. Tips:邻接矩阵与邻接表相比,疏松图多用邻接表,稠密图多用邻接矩阵. 邻接矩阵: 开一个二维数组gr ...

  7. 【模板】链式前向星+spfa

    洛谷传送门--分糖果 博客--链式前向星 团队中一道题,数据很大,只能用链式前向星存储,spfa求单源最短路. 可做模板. #include <cstdio> #include <q ...

  8. zzuli 2131 Can Win dinic+链式前向星(难点:抽象出网络模型+建边)

    2131: Can Win Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 431  Solved: 50 SubmitStatusWeb Board ...

  9. HDU1532 Drainage Ditches SAP+链式前向星

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. spring boot 中事物的使用

    一.什么是事务? 事务,通俗的说就是,同时做多个事,要么全做,要么不做,也是其特性.举个例子来说,好比你在某宝.某东.某多上购物,在你提交订单的时候,库存也会相应减少,不可能是钱付了,库存不减少,或者 ...

  2. Spring Boot中路径及配置文件读取问题

    编译时src/main/java中*.java文件会被编译成*.class文件,在classpath中创建对应目录及class文件           src/main/resources目录中的文件 ...

  3. 【题解】CF742E (二分图+构造)

    [题解]CF742E (二分图+构造) 自闭了CodeForces - 742E 给定的条件就是一个二分图的模型,但是有一些不同.不同就不同在可以出现相邻两个点颜色相同的情况. 构造常用方法之一是按奇 ...

  4. 「CF670C」Cinema 解题报告

    题面 传送门 思路: 离散化.hash 对于这样一个明显的统计排序的题目,当然轻而易举啦~ 但是!看!语言的编号 a数组和 b数组的值最大在\(10^9\)的级别,所以开个数组来存---That's ...

  5. 1071 小赌怡情 (15分)C语言

    常言道"小赌怡情".这是一个很简单的小游戏:首先由计算机给出第一个整数:然后玩家下注赌第二个整数将会比第一个数大还是小:玩家下注 t 个筹码后,计算机给出第二个数.若玩家猜对了,则 ...

  6. Vue+Vant+Vuex实现本地购物车功能

    通常,我们做移动端商城的时候,通常会有购物车模块,那购物车模块有两种实现方式,一是储存在后台通过接口获取到购物车信息,二是存在用户手机本地,第一种方法只要调用接口获取比较简单,这里介绍的是第二种方法, ...

  7. oracle中使用pl/sql进行的文件读写操作

    第一次知道,可以使用pl/sql来进行文件的读写操作,嘿嘿,简单的试了下可行. 基本步骤如下: SQL> conn sys/sys@orcl as sysdba 已连接. SQL> cre ...

  8. Django常用字段及参数、事务、数据库查询优化

    常用字段 注意: Django中没有设置对应char类型的字段,但可以支持自己定义. 自定义对应于数据库的char类型字段: from django.db.models import Field cl ...

  9. Ant Design框架中不同的组件访问不同的models中的数据

    Ant Design框架中不同的组件访问不同的models中的数据 本文记录了我在使用该框架的时候踩过的坑,方便以后查阅. 一.models绑定 在某个组件(控件或是页面),要想从某个models中获 ...

  10. cc协议(知识共享,Creative Commons),程序员的基础守则之一

    知识共享 我在浏览git开源代码的时候,浏览到一句话: 版权声明:本文为CSDN博主「...」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明. 原文链接:http ...