HDU 2647 Reward
2014-08-29 16:30
169 查看
Reward
Time Limit: 1000msMemory Limit: 32768KB
This problem will be judged on HDU. Original ID: 2647
64-bit integer IO format: %I64d Java class name: Main
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
Source
曾是惊鸿照影来解题:拓扑排序。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <vector> #include <queue> #include <cstdlib> #include <string> #include <set> #include <stack> #define LL long long #define pii pair<int,int> #define INF 0x3f3f3f3f using namespace std; const int maxn = 10010; int n,m,in[maxn],money[maxn]; vector<int>g[maxn]; queue<int>q; int topSort() { int ans = 0; while(!q.empty()) q.pop(); for(int i = 1; i <= n; i++) { if(!in[i]) { q.push(i); money[i] = 888; } } int sum = n; while(!q.empty()){ sum--; int u = q.front(); q.pop(); for(int i = 0; i < g[u].size(); i++){ if(--in[g[u][i]] == 0){ q.push(g[u][i]); money[g[u][i]] = money[u]+1; } } } if(sum > 0) return -1; else{ for(int i = 1; i <= n; i++) ans += money[i]; return ans; } } int main() { int i,u,v; while(~scanf("%d %d",&n,&m)) { for(i = 1; i <= n; i++) { g[i].clear(); in[i] = 0; money[i] = 0; } for(i = 0; i < m; i++) { scanf("%d %d",&v,&u); g[u].push_back(v); in[v]++; } printf("%d\n",topSort()); } return 0; }
View Code
相关文章推荐
- HDU 2647 Reward【拓扑排序】
- HDU 2647:Reward
- hdu 2647 Reward
- 拓扑排序学习小记 HDU 1285 确定比赛名次 + HDU 2647 Reward
- HDU 2647 Reward(图论-拓扑排序)
- hdu 2647 Reward(拓扑排序)
- HDU-2647-Reward
- HDU - 2647 Reward 拓扑排序
- HDU 2647 Reward(拓扑排序)
- HDU 2647 Reward
- HDU 2647 Reward (拓扑排序)
- hdu 2647 Reward(拓扑排序)
- hdu 2647 Reward (拓扑排序)
- HDU 2647 Reward(拓扑排序)
- HDU 2647 Reward(反向拓扑)
- HDU 2647 Reward【拓扑排序】
- HDU 2647 Reward
- hdu 2647 Reward(拓扑排序)
- HDU_2647_Reward(拓扑排序)
- HDU 2647 Reward