HDU-2647 Reward(链式前向星+拓扑排序)
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(链式前向星+拓扑排序)的更多相关文章
- 洛谷 P1352 没有上司的舞会【树形DP/邻接链表+链式前向星】
题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri, ...
- HDU 2544最短路 【dijkstra 链式前向星+优先队列优化】
最开始学最短路的时候只会用map二维数组存图,那个时候还不知道这就是矩阵存图,也不懂得效率怎么样 经过几个月的历练再回头看最短路的题, 发现图可以用链式前向星来存, 链式前向星的效率是比较高的.对于查 ...
- hdu2647 逆拓扑,链式前向星。
pid=2647">原文地址 题目分析 题意 老板发工资,可是要保证发的工资数满足每一个人的期望,比方A期望工资大于B,仅仅需比B多1元钱就可以.老板发的最低工资为888元.输出老板最 ...
- 链式前向星+SPFA
今天听说vector不开o2是数组时间复杂度常数的1.5倍,瞬间吓傻.然后就问好的图表达方式,然后看到了链式前向星.于是就写了一段链式前向星+SPFA的,和普通的vector+SPFA的对拍了下,速度 ...
- 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板
一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...
- 图的存储结构:邻接矩阵(邻接表)&链式前向星
[概念]疏松图&稠密图: 疏松图指,点连接的边不多的图,反之(点连接的边多)则为稠密图. Tips:邻接矩阵与邻接表相比,疏松图多用邻接表,稠密图多用邻接矩阵. 邻接矩阵: 开一个二维数组gr ...
- 【模板】链式前向星+spfa
洛谷传送门--分糖果 博客--链式前向星 团队中一道题,数据很大,只能用链式前向星存储,spfa求单源最短路. 可做模板. #include <cstdio> #include <q ...
- zzuli 2131 Can Win dinic+链式前向星(难点:抽象出网络模型+建边)
2131: Can Win Time Limit: 1 Sec Memory Limit: 128 MB Submit: 431 Solved: 50 SubmitStatusWeb Board ...
- HDU1532 Drainage Ditches SAP+链式前向星
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- Iptables-linux服务器做路由转发
https://blog.csdn.net/liang_operations/article/details/80747510 实现内部服务器C可以经过服务器B进行上网. 3.1服务器双网卡,一块配置 ...
- [reviewcode] 那些基础comments
多次提醒我,为变量取个合适的名字, so cute person: Not a big deal, but try using variable names better than my_sa 每个参 ...
- mysql中information_schema.views字段说明
1.查看视图并不是查询视图数据,而是查看数据库中已经存在的视图的定义,查看视图必须要有SHOW VIEW权限,MySQL的数据库下的user表中存储这这个数据.查看视图的方法有:DESCRIBE,SH ...
- PAC 代理自动发现简介
一 简介 1.1 什么是PAC文件 代理自动配置(PAC)文件包含一组用javaScript编码的规则,允许web浏览器确定是将Web流量直接发送到Internet还是通过代理服务器发送 ...
- DEVOPS技术实践_04:Jenkins参数化构建
一.参数化构建 1.1 各个参数的信息 凭据参数存储一个用户的账号密码信息,等等,运用最多的是选项参数 1.2 使用选项参数 构建已经变成参数化构建 1.3 获取这个值,修改Jenkinsfile文件 ...
- CountDownLanuch,CyclicBarrier,Semaphore,Lock
一.你在项目中用过CountDownLanuch,CyclicBarrier,Semaphore吗? 1.CountDownLanuch是一个同步的工具类,它允许一个或多个线程一直等待,直到其他线程执 ...
- CAS的实现原理
CAS的全称是CompareAndSwap,比较并交换,是Java保证原子性的一种重要方法,也是一种乐观锁的实现方式. 它需要先提前一步获取旧值,然后进入此方法比较当下的值是否与旧值相同,如果相同,则 ...
- 利用 Hexo 或者 hugo 搭建个人博客
我们无法选择生活的样子,但我们可以记下来. 博客的开始 其实,一切都是为了更好的装逼.好吧,我着相了. 最开始想做一个自己博客,主要是因为看到了很多人都有,觉得自己没有太 Low 了.于是申请了 CS ...
- three.js入门第一个案例
准备工作 1.运用three.js进行3d开发,其实和页面编程一样,首先需要在html文件中引入three.js.Three.js使用面向对象的方式来构建程序,它包含3个基本对象: 场景(scene) ...
- docker使用阿里云加速器
1 登录阿里云获得地址 登录https://cr.console.aliyun.com ,点击"镜像加速器",会给我一个地址. 2 写入/etc/docker/daemon.jso ...