题意不说了,说下思路。

给出的关系是a要求的工资要比b的工资多,因为尽可能的让老板少付钱,那么a的工资就是b的工资+1。能够确定关系为a>b,依据拓扑排序建边的原则是把“小于”关系看成有向边。那么我们能够建边v->u。

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <map>
#include <queue>
#include <stack>
#include <set>
#define M 10000+5
#define LL long long
#define Ld __int64
#define eps 0.00001
#define INF 999999999
#define MOD 112233
#define MAX 26
#define PI acos(-1.0)
using namespace std; vector<int> G[M];
int num[M];
int n;
int into[M],ans; int toposort()
{
queue<int> q;
int cnt=0;
ans=0;
for(int i=1;i<=n;i++)
{
if(into[i]==0) //入度为0的点进入队列中
q.push(i);
}
while(!q.empty())
{
cnt++;
int u=q.front();
ans+=num[u];
q.pop();
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(--into[v]==0) //若删除(u,v)这条边之后,v的入度也为0,则压入队列中
{
q.push(v);
num[v]=num[u]+1; //v要求的工资比u高
}
}
}
if(cnt!=n) //推断有无环。-1表示有环。1表示无环
ans=-1;
return ans;
} int main()
{
int m;
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<=n;i++)
{
G[i].clear();
into[i]=0;
num[i]=888;
}
while(m--)
{
int u,v;
scanf("%d%d",&u,&v);
G[v].push_back(u);
into[u]++;
}
printf("%d\n",toposort());
}
return 0;
} /* 5 4
2 1
2 5
5 3
3 4 */

HDU2647(拓扑排序+反向建图)的更多相关文章

  1. HDU 2647 Reward 【拓扑排序反向建图+队列】

    题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...

  2. HDU 4857 逃生 【拓扑排序+反向建图+优先队列】

    逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  3. hdoj--4857--逃生(拓扑排序+反向建图)

    逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  4. Educational Codeforces Round 25 E. Minimal Labels 拓扑排序+逆向建图

    E. Minimal Labels time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. hdu 4857 逃生 拓扑排序+逆向建图

    逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Descr ...

  6. HUD2647 Reward_反向建图拓扑排序

    HDU2647 Reward 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意:老板要发奖金了,有n个人,给你m对数,类似a b,这样的一对 ...

  7. HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)

    逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...

  8. POJ3687——Labeling Balls(反向建图+拓扑排序)

    Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...

  9. 逃生 HDU 4857(反向建图 + 拓扑排序)

    逃生 链接 Problem Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必 ...

随机推荐

  1. 【转】C# String.Format数字格式化输出各种转换{0:N2} {0:D2} {0:C2}...

    ; //格式为sring输出 // Label1.Text = string.Format("asdfadsf{0}adsfasdf",a); // Label2.Text = & ...

  2. Java Executor 框架

    Java Executor 框架 Executor框架是指java5中引入的一系列并发库中与executor相关的功能类,包括Executor.Executors. ExecutorService.C ...

  3. 用EnableMenuItem不能使菜单变灰的原因

    为何不能Disable菜单项     问:我有一个工具button在WM_COMMAND消息是这样做的:         CMenu   *pMenu   =   GetMenu();     pMe ...

  4. Python学习入门基础教程(learning Python)--3.3.3 Python逻辑关系表达式

    在if分支判断语句里的条件判断语句不一定就是一个表达式,可以是多个(布尔)表达式的组合关系运算,这里如何使用更多的关系表达式构建出一个比较复杂的条件判断呢?这里需要再了解一下逻辑运算的基础知识.逻辑关 ...

  5. python之字符串的分割和拼接

    关于string的split 和 join 方法 对导入os模块进行os.path.splie()/os.path.join() 貌似是处理机制不一样,但是功能上一样. 1.string.split( ...

  6. [Cacti] cacti监控mongodb性能实战

    .生成监控图 在界面.选择"Device".选择mongodb服务器连接"3.X2_mongodb".再选择右上角的"Create Graphs fo ...

  7. POJ3279 Catch That Cow(BFS)

    本文来源于:http://blog.csdn.net/svitter 意甲冠军:给你一个数字n, 一个数字k.分别代表主人的位置和奶牛的位置,主任能够移动的方案有x+1, x-1, 2*x.求主人找到 ...

  8. JAVA 数据权限设计

    数据权限设计 前言 在各种系统中.要保证数据对象的安全性以及易操作性,使企业的各业务部门.职能部门可以方便并且高效的协同工作,那么一个好的数据权限管理设计就成为一个关键的问题.尽管企业中各个单元的工作 ...

  9. SIP基本呼叫

    我们首先来看下主要的呼叫流程. INVITEsip:69690067@beijing.chinamobile.com;user=phone SIP/2.0 From:"+8610696900 ...

  10. 静态方法使用bean

    java类中的代码 public class BidMsgUtil { private static Logger log = Logger.getLogger(BidMsgUtil.class); ...