PKU 3275 Ranking the Cows 最短路 floyd
2016-05-05 16:58
441 查看
题意/Description:
Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a different positive rate, and FJ would like to order his cows according to these rates from the fastest milk
producer to the slowest.
FJ has already compared the milk output rate for M (1 ≤ M ≤ 10,000) pairs of cows. He wants to make a list of C additional pairs of cows such that, if he now compares
those C pairs, he will definitely be able to deduce the correct ordering of all N cows. Please help him determine the minimum value of C for which such a list is possible.
读入/Input:
Line 1:
Two space-separated integers: N and M
Lines 2..M+1: Two space-separated integers, respectively: X and Y.
Both X and Y are in the range 1...N and
describe a comparison where cow X was ranked higher than cow Y.
输出/Output:
Line 1: A single integer that is the minimum value of C.
题解/solution:
如果n头牛排序完成,应该有C(n,2)关系已知,即任意两头牛的速度都知道了。然后可以从已经比较了的m对牛中算出可以推导出多少对牛已经知道了,推导方法可以参考floyd最短路,然后用C(n,2)减去它就是答案。
补充:C(N, 2) = N * (N - 1) / 2
调查K对关系:C(N, 2) - K
代码/Code:
Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a different positive rate, and FJ would like to order his cows according to these rates from the fastest milk
producer to the slowest.
FJ has already compared the milk output rate for M (1 ≤ M ≤ 10,000) pairs of cows. He wants to make a list of C additional pairs of cows such that, if he now compares
those C pairs, he will definitely be able to deduce the correct ordering of all N cows. Please help him determine the minimum value of C for which such a list is possible.
读入/Input:
Line 1:
Two space-separated integers: N and M
Lines 2..M+1: Two space-separated integers, respectively: X and Y.
Both X and Y are in the range 1...N and
describe a comparison where cow X was ranked higher than cow Y.
输出/Output:
Line 1: A single integer that is the minimum value of C.
题解/solution:
如果n头牛排序完成,应该有C(n,2)关系已知,即任意两头牛的速度都知道了。然后可以从已经比较了的m对牛中算出可以推导出多少对牛已经知道了,推导方法可以参考floyd最短路,然后用C(n,2)减去它就是答案。
补充:C(N, 2) = N * (N - 1) / 2
调查K对关系:C(N, 2) - K
代码/Code:
var a,b:array [0..10001,0..1001] of longint; f:array [0..1001,0..1001] of boolean; n,m,ans:longint; procedure init; var i,x,y:longint; begin readln(n,m); for i:=1 to m do begin readln(x,y); f[x,y]:=true; inc(a[x,0]); inc(b[y,0]); a[x,a[x,0]]:=y; b[y,b[y,0]]:=x; end; ans:=0; end; procedure main; var i,j,k,x,y:longint; begin for k:=1 to n do for i:=1 to b[k,0] do begin x:=b[k,i]; for j:=1 to a[k,0] do begin y:=a[k,j]; if not f[x,y] then begin f[x,y]:=true; inc(a[x,0]); inc(b[y,0]); a[x,a[x,0]]:=y; b[y,b[y,0]]:=x; inc(ans); end; end; end; write(n*(n-1) div 2-ans-m); end; begin init; main; end.
相关文章推荐
- PKU 3259 Wormholes 最短路 bellman
- PKU 1860 Currency Exchange 最短路 bellman
- PKU 3013 Big Christmas Tree 最短路 spfa
- PKU 3311 Hie with the Pie 最短路 folyd
- Priest John's Busiest Day poj 3683 tarjan+2-SAT
- Get Luffy Out poj 2723 Tarjan+2-SAT
- Ikki's Story IV - Panda's Trick poj3207 tarjan+2-SAT
- Katu Puzzle poj 3678 kosaraju+2-SAT
- Popular Cows poj 2186 tarjan
- Network of Schools poj 1236 Kosrarju
- 选课 topsort
- poj 3378 Crazy Thairs
- poj 2182 Lost Cows 线段树
- poj 2828 Buy Tickets 线段树
- poj 2777 Count Color 线段树
- POJ 3321 Apple Tree 线段树
- 沼泽鳄鱼 矩阵乘法
- 奶牛接力 矩阵乘法
- 家谱 并差集
- 昂贵的聘礼 最短路 dijkstra