codeup24699 奖金
题目描述
1. 奖金[reward. pas/c/cpp]
【问题描述】
老胡决定以每个人本年在信息学奥赛中的贡献为标准来发放队员们的奖金。
于是老胡召开m方会谈。每位参加会谈的队员提出了自己的意见:“我认为队员a的奖金应该比b高!”老胡决定要找出一种奖金方案,满足各位队员的意见,且同时使得总奖金数最少。每位队员奖金最少为100元。
【输入数据】
第一行两个整数n,m,表示队员总数和代表数;
以下m行,每行2个整数a,b,表示某个代表认为第a号队员奖金应该比第b号队员高。
【输出数据】
若无法找到合法方案,则输出“Poor Xed”;否则输出一个数表示最少总奖金。
【输入样例】
2 1
1 2
【输出样例】
201
【数据规模】
80%的数据满足n<=1000,m<=2000;
100%的数据满足n<=10000,m<=20000。
http://blog.csdn.net/u010858667/article/details/10336315 神奇的思路。。。看起来是分层图dp?。。。好像叫做拓扑排序。。。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
const int N = 20020;
typedef long long ll;
int n, m, a[N], b[N], modify, len;
int head[N], to[N], rest[N], tot;
int vis[N], in[N];
ll cost[N], ans;
queue<int> q;
void add(int u, int v) {
tot ++;
to[tot] = v;
rest[tot] = head[u];
head[u] = tot;
}
void bfs() {
for(int i = 1 ; i <= n ; i ++) {
cost[i] = 100;
}
while(1) {
for(int i = 1 ; i <= n ; i ++) {
if(in[i] == 0 && vis[i] == 0) {
q.push(i);
vis[i] = 1;
}
}
if(q.empty()) break;
while(q.size()) {
int u = q.front(); q.pop();
modify ++;
cost[u] += len;
for(int i = head[u] ; i ; i = rest[i]) {
int v = to[i];
in[v] --;
}
}
len ++;
}
}
int main() {
scanf("%d%d", &n, &m);
for(int i = 1 ; i <= m ; i ++) {
scanf("%d%d", &a[i], &b[i]);
add(b[i], a[i]);
in[a[i]] ++;
}
bfs();
if(modify == n) {
for(int i = 1 ; i <= n ; i ++) {
ans += cost[i];
}
printf("%lld\n", ans);
} else {
puts("Poor Xed");
}
}

浙公网安备 33010602011771号