HDU2647(拓扑排序+反向建图)
题意不说了,说下思路。
给出的关系是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(拓扑排序+反向建图)的更多相关文章
- HDU 2647 Reward 【拓扑排序反向建图+队列】
题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...
- HDU 4857 逃生 【拓扑排序+反向建图+优先队列】
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- hdoj--4857--逃生(拓扑排序+反向建图)
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...
- 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 ...
- hdu 4857 逃生 拓扑排序+逆向建图
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descr ...
- HUD2647 Reward_反向建图拓扑排序
HDU2647 Reward 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意:老板要发奖金了,有n个人,给你m对数,类似a b,这样的一对 ...
- HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)
逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...
- POJ3687——Labeling Balls(反向建图+拓扑排序)
Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...
- 逃生 HDU 4857(反向建图 + 拓扑排序)
逃生 链接 Problem Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必 ...
随机推荐
- OpenStack_Swift源代码分析——ObjectReplicator源代码分析(2)
1.Replicator运行代码具体分析 上篇问中介绍了启动Replicator的详细过程,以下解说Replicator的运行代码的详细实现,首先看replicate方法: def replicate ...
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
- 辛星与您使用CSS导航条
第一步.我们创建了一个新的my.html档.在内容填入如下面.这个html文件不动,直到最后.正是这些内容: <!DOCTYPE html PUBLIC "-//W3C//DTD XH ...
- codeforces 459D - Pashmak and Parmida's problem【离散化+处理+逆序对】
题目:codeforces 459D - Pashmak and Parmida's problem 题意:给出n个数ai.然后定义f(l, r, x) 为ak = x,且l<=k<=r, ...
- 关于Hbase的cache配置
关于Hbase的cache配置 在hbase中的hfilecache中,0.96版本号中新添加了bucket cache, bucket cache通过把hbase.offheapcache.perc ...
- uvalive4327(单调队列优化)
这题我有闪过是用单调队列优化的想法,也想过有左右两边各烧一遍. 但是不敢确定,搜了题解,发现真的是用单调队列,然后写了好久,调了好久下标应该怎么变化才过的. dp[i][j] 表示走到第i行,第j个竖 ...
- AC automation 模板
/* 1.对n个字符串构造tire树 insertWord(node *root, char *word); 2.bfs构造fail指针 makeFail(node *root); 3.基于以上两点的 ...
- hdu1561(树形背包)
给定n,m表示n个城堡,我们可以选择攻占m个城堡.要使得价值最大 接下来n行 a b, 第i行的a b,表示攻占第i个城堡的价值为b,但需要先攻占第a个城堡 如果有多个a=0的点,那么就不是一棵树 ...
- Spark 1.0.0 横空出世 Spark on Yarn 部署(Hadoop 2.4)
就在昨天,北京时间5月30日20点多.Spark 1.0.0最终公布了:Spark 1.0.0 released 依据官网描写叙述,Spark 1.0.0支持SQL编写:Spark SQL Progr ...
- 【原创】shadowebdict开发日记:基于linux的简明英汉字典(二)
全系列目录: [原创]shadowebdict开发日记:基于linux的简明英汉字典(一) [原创]shadowebdict开发日记:基于linux的简明英汉字典(二) [原创]shadowebdic ...