HDU 2647 Reward (拓扑排序)
2015-11-03 23:06
453 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647
题面:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6207 Accepted Submission(s): 1918
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
Author
dandelion
题目大意:
老板发奖金,每人最少888元,有一些要求。a b,a的工资要比b的工资高。如果存在矛盾的关系,则输出-1,否则输出最好需要多少钱。
解题:
比较明显的拓扑排序,与其他题目不同的是,节点较多,需用邻接表。同时需要给每个人分个权值,一个点可能会有多个点比它小,这时需要取所有它的权值中的最大值,确保它大于所有比它小的值。
代码:
题面:
Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6207 Accepted Submission(s): 1918
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
Author
dandelion
题目大意:
老板发奖金,每人最少888元,有一些要求。a b,a的工资要比b的工资高。如果存在矛盾的关系,则输出-1,否则输出最好需要多少钱。
解题:
比较明显的拓扑排序,与其他题目不同的是,节点较多,需用邻接表。同时需要给每个人分个权值,一个点可能会有多个点比它小,这时需要取所有它的权值中的最大值,确保它大于所有比它小的值。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <set> #include <queue> using namespace std; //s集合排重边,据说没有 set <int> s; //分别为权值,入度,邻接表的头位置,cnt邻接表的计数值 int val[10010],in[10010],head[10010],cnt=0; //访问标记 bool vis[10010]; //边存储 struct edge { int to,next; }ee[20010]; //加边操作 void add_edge(int a,int b) { ee[cnt].to=b; //接上后续地址 ee[cnt].next=head[a]; head[a]=cnt++; } queue <int> qe; int max(int a,int b) { return a>b?a:b; } int main() { int n,m,a,b,tmp,ans,cur,v,to; bool flag; while(~scanf("%d%d",&n,&m)) { flag=true; cnt=0; ans=0; while(!qe.empty()) qe.pop(); s.clear(); //赋初值 for(int i=1;i<=n;i++) val[i]=888; //初始化 memset(in,0,sizeof(in)); memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); for(int i=0;i<m;i++) { //这写反了,wa了一次 scanf("%d%d",&b,&a); tmp=a*10000+b; //排重 if(!s.count(tmp)) { //加边,统计入度,最小的点,入度为0 add_edge(a,b); in[b]++; } } for(int i=1;i<=n;i++) { if(in[i]==0) { qe.push(i); ans+=888; vis[i]=1; } } while(!qe.empty()) { cur=qe.front(); qe.pop(); v=val[cur]+1; for(int i=head[cur];i!=-1;i=ee[i].next) { to=ee[i].to; //取最大值,以确保满足 val[to]=max(val[to],v); //不断加入入度为0的点 in[to]--; if(in[to]==0) { ans+=val[to]; vis[to]=1; qe.push(to); } } } //如果还有没访问过的点,就是存在矛盾 for(int i=1;i<=n;i++) { if(!vis[i]) { flag=false; break; } } if(flag) printf("%d\n",ans); else printf("-1\n"); } return 0; }
相关文章推荐
- 详解图的应用(最小生成树、拓扑排序、关键路径、最短路径)
- 初学图论-Kahn拓扑排序算法(Kahn's Topological Sort Algorithm)
- 初学图论-Bellman-Ford单源最短路径算法
- 初学图论-DAG单源最短路径算法
- 初学图论-Dijkstra单源最短路径算法
- 初学图论-Dijkstra单源最短路径算法基于优先级队列(Priority Queue)的实现
- 封装好的Folyd建图,C++源码
- LCA模板
- 图论学习笔记之一——Floyd算法
- 【LCA】SPOJ QTREE2
- poj 3249 Test for Job 最长路
- HDU 3342
- HDU 2544
- Timus 1557 Network Attack DFS+各种各种...
- HDU1289 Tarjan-模板题
- Poj2638 网络流+最短路+二分答案
- Aizu1311 分层图最短路 (...大概)
- HDU 3631 Shortest Path
- 二分图匹配模板
- Dijkstra算法