http://acm.hdu.edu.cn/showproblem.php?pid=2647

#include <stdio.h>
#include <string.h>
#include <queue>
#define LL long long
using namespace std;
int head[],cnt=;
int val[];
int in[];
int n,m,num;
queue<int>q;
struct node
{
int u,v;
int next;
} edge[];
void add(int u,int v)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}
void init()
{
cnt = ,num = ;
while(!q.empty()) q.pop();
memset(in,,sizeof(in));
memset(head,-,sizeof(head));
}
void toposort()
{
for (int i = ; i <= n; i++)
{
if (in[i]==)
{
q.push(i);
val[i]=;
}
}
while(!q.empty())
{
int u = q.front();
q.pop();
num++;
for (int j = head[u]; j!=-; j=edge[j].next)
{
int v = edge[j].v;
val[v] = val[v] <= val[u]+? val[u]+:val[u];
if (val[v] <= val[u])
val[v] = val[u]+;
in[v]--;
if(!in[v])
q.push(v);
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
int u,v;
for (int i = ; i < m; i++)
{
scanf("%d%d",&u,&v);
add(v,u);
in[u]++;
}
toposort();
if(num < n)
{
puts("-1");
continue;
}
LL ans = ;
for (int i = ; i <= n; i++)
ans += val[i];
printf("%lld\n",ans);
}
return ;
}

Reward(toposort)的更多相关文章

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

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

  2. hdu 2647 Reward(拓扑排序,反着来)

    Reward Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submis ...

  3. HDU 2647:Reward(拓扑排序+队列)

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  4. Reward HDU - 2647

    传送门     Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to dis ...

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

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

  6. poj 1270(toposort)

    http://poj.org/problem?id=1270 题意:给一个字符串,然后再给你一些规则,要你把所有的情况都按照字典序进行输出. 思路:很明显这肯定要用到拓扑排序,当然看到discuss里 ...

  7. 拓扑排序 POJ2367Genealogical tree[topo-sort]

    ---恢复内容开始--- Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4875   A ...

  8. POJ 1094 (TopoSort)

    http://poj.org/problem?id=1094 题意:该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序 ...

  9. 扩展KMP --- HDU 3613 Best Reward

    Best Reward Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...

随机推荐

  1. Luogu P1311 选择客栈

    暴力 我一开始做这道题先想到的就是暴力... 所以先说一下暴力的做法.首先在输入的时候讲花费小于P的位置标记下来,然后用两层循环枚举所有的两个客栈的组合方案.再用一层循环将两个客栈之间的位置扫一遍,如 ...

  2. idea必选配置

    参考: IDEA配置

  3. MyBatis 的基本要素—核心对象

    MyBatis 三个基本要素   ➢ 核心接口和类 ➢ MyBatis 核心配置文件(mybatis-config.xml) ➢ SQL 映射文件(mapper.xml) MyBatis 核心接口和类 ...

  4. Linux学习笔记记录(二)

  5. HP下kafka的实践

    kafka 简介 Kafka 是一种高吞吐量的分布式发布订阅消息系统 kafka角色必知 producer:生产者. consumer:消费者. topic: 消息以topic为类别记录,Kafka将 ...

  6. Linux - 模块编程初试

    计算机网络的课程设计要做防火墙,老师没有限制在什么系统上面做,所以决定在Linux上实现.找了一下相关的资料,发现其实Linux有提供Netfilter/Iptables,为用户提供防火墙的功能,稍微 ...

  7. [COGS311] Redundant Paths

    ★★☆   输入文件:rpaths.in   输出文件:rpaths.out   简单对比 时间限制:1 s   内存限制:128 MB Description In order to get fro ...

  8. 你的ExcelUtil简单、高效、易扩展吗

    你的ExcelUtil简单.高效.易扩展吗 Author: Dorae Date: 2018年10月23日12:30:15 转载请注明出处 一.背景 最近接到了和Excel导出相关的需求,但是: 项目 ...

  9. 最小生成树 E - QS Network

    Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the planet w-5 ...

  10. Effictive Java学习笔记1:创建和销毁对象

    建议1:考虑用静态工厂方法代替构造器 理由:1)静态方法有名字啊,更容易懂和理解.构造方法重载容易让人混淆,并不是好主意 2)静态工厂方法可以不必每次调用时都创建一个新对象,而公共构造函数每次调用都会 ...