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必 ...
随机推荐
- 行为驱动开发(BDD)
行为驱动开发(BDD) 引言 BDD是对TDD理念的扩展.BDD强调有利害关系的技术团体和非技术团队都要参与到软件开发过程中.可以把它看成一种强调团体间合作的敏捷方法.大多数采用某种敏捷方法的团队最终 ...
- php文件操作基本使用方法
<?php /* $fp=fopen("tmp.html","r"); $str=fread($fp,filesize("tmp.html&qu ...
- PHP读取Excel里的文件
下载phpExcelReader http://sourceforge.net/projects/phpexcelreader 解压后得到以下这些文件 jxlrwtest.xls这个excel文件有 ...
- 四个漂亮CSS样式表
1. 单像素的边框CSS表格 这是一个非常所用的表格风格. 源码: <!-- CSS goes in the document HEAD or added to your external st ...
- windows中间vmware的Linux系统安装jdk步骤
1.设置文件的享受,对于本地阅读windows档 于vmware虚拟机设置共享文件夹,那么共享文件中,你可以 2.然后打开虚拟机上,使用root输入账户,然后,在夹/mnt/hgfs/ 共享文件了 ...
- Directx11学习笔记【十八】 Blending混合
本文由zhangbaochong原创,转载请注明出处http://www.cnblogs.com/zhangbaochong/p/5634580.html 在d3d11中是按frame来渲染物体的,在 ...
- java多线程Future和Callable类的解释与使用
一,描写叙述 在多线程下编程的时候.大家可能会遇到一种需求,就是我想在我开启的线程都结束时,同一时候获取每一个线程中返回的数据然后再做统一处理,在这种需求下,Future与Callable的组合就派 ...
- .Net下一个Winform方案可以让MessageBox.Show它显示在父窗口的中间
下面的文字,缺省值是在屏幕中间显示. DialogResult dr = MessageBox.Show("是否要删除此数据?", "删除确认", Messag ...
- Zygote过程【3】——SystemServer诞生
欢迎转载.转载请注明:http://blog.csdn.net/zhgxhuaa 在ZygoteInit的main()方法中做了几件大事.当中一件便是启动Systemserver进程.代码例如以下: ...
- 移动web:翻页场景动画
在移动web,特别是在微信中,经常看到一种翻页动画效果,也称为场景动画. 一页一页的翻过,像在看书,每页的内容以各种"炫酷"的效果出现在你的眼里,配上一首动听的音乐,你有没有喜欢上 ...