hdu 2643 拓扑排序加优先队列
来源:互联网 发布:lol末日人工智能攻略 编辑:程序博客网 时间:2024/05/17 03:34
Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5345 Accepted Submission(s): 1621
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.
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.
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 11 22 21 22 1
Sample Output
1777-1
Author
dandelion
Source
曾是惊鸿照影来
加优先队列的拓扑排序,,,就不说啥了
#include<map>#include<vector>#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<stack>#include<queue>#include<set>#define inf 0x3f3f3f3f#define mem(a,x) memset(a,x,sizeof(a))using namespace std;typedef long long ll;typedef pair<int,int> pii;inline int in(){ int res=0;char c; while((c=getchar())<'0' || c>'9'); while(c>='0' && c<='9')res=res*10+c-'0',c=getchar(); return res;}priority_queue<pii,vector<pii>,greater<pii> > q;vector<int> v[10006];int rd[10206];int money[10006];int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { for(int i=0;i<=n;i++) { v[i].clear(); } mem(rd,0); for(int i=0;i<m;i++) { int t1=in(),t2=in(); v[t2].push_back(t1); rd[t1]++; } mem(money,0); for(int i=1;i<=n;i++) { if(rd[i]==0)q.push(pii(0,i)); } while(!q.empty()) { pii now=q.top(); q.pop(); int x=now.second; for(int i=0;i<(int)v[x].size();i++) { int t=v[x][i]; money[t]=now.first+1; rd[t]--; if(rd[t]==0)q.push(pii(money[t],t)); } } bool ok=1; for(int i=1;i<=n;i++)if(rd[i]>0) { ok=0; break; } if(!ok) { puts("-1"); continue; } int ans=888*n; for(int i=1;i<=n;i++)ans+=money[i]; cout<<ans<<endl; } return 0;}
0 0
- hdu 2643 拓扑排序加优先队列
- HDU 4857 逃生 拓扑排序+优先队列
- HDU 5695 (拓扑排序 优先队列)
- HDU 1285 拓扑排序+优先队列
- hdu 4857 反向拓扑排序+优先队列
- HDU 5638 Toposort 拓扑排序 优先队列
- HDU 1285 <优先队列+拓扑排序> <拓扑模版>
- 拓扑排序+优先队列
- 拓扑排序+优先队列
- HDU 1285 确定比赛名次(拓扑排序+优先队列)
- HDU 4857 (反向拓扑排序 + 优先队列)
- hdu 4857 逃生 拓扑排序+优先队列,逆向处理
- hdu 4857 逃生(优先队列+逆拓扑排序)
- HDU 4857 逃生 【拓扑排序+反向建图+优先队列】
- 拓扑排序模板-优先队列 hdu 1285 确定比赛名次
- HDU-5695-Gym Class(拓扑排序+优先队列)
- hdu 5695 Gym Class【拓扑排序+优先队列】
- HDU 5695 Gym Class (拓扑排序+优先队列)
- 深入理解java字节流与字符流
- MC&Redis&Ehcache介绍
- popupwindow弹出后,屏幕背景变成半透明
- C/C++函数指针与回调函数
- 如何处理ul/ol下li左边有段空白的样式问题
- hdu 2643 拓扑排序加优先队列
- cocos2d-x 3.2 android手机震动
- java异常处理
- Java 固话和手机归属地判断
- 项目总结——传说中的反射竟然是这个样子
- CPU缓存刷新的误解
- JRebel+myeclipse 热部署
- HBase - MapReduce - 使用 MapReduce 批量操作 HBase 介绍 | 那伊抹微笑
- C++黑客编程揭秘与防范(第2版)——互动出版网