Cow Contest POJ - 3660 Floyd算法,关系链图
N?(1 ≤?N?≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors.
The contest is conducted in several head-to-head rounds, each between two cows. If cow?A?has a greater skill level than cow?B?(1 ≤?A?≤?N; 1 ≤?B?≤?N;?A?≠?B), then cow?A?will always beat cow?B.
Farmer John is trying to rank the cows by skill level. Given a list the results of?M?(1 ≤?M?≤ 4,500) two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradictory.
Input
* Line 1: Two space-separated integers:?N?and?M
* Lines 2..M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer,?A, is the winner) of a single round of competition:?A?and?B
Output
* Line 1: A single integer representing the number of cows whose ranks can be determined
Sample Input
5 5 4 3 4 2 3 2 1 2 2 5Sample Output
2
題意:有n頭奶牛,依次標號為1~n,每一頭奶牛都有一個能力值(不重復)。現在兩兩之間不重復的進行m場battle,自然,能力值高的奶牛將戰勝能力值低的奶牛。給出m場battle的最終勝負結果(a,b)(a為勝者),問:根據已知結 果,有多少頭奶牛在整體中的排名能被確定。
思路:首先得明確一個點,。最短路一般是求最短的路徑或時間或其他,而這個題卻是用
最短路的思想來判斷兩頭奶牛之間的關聯,首先一個數組ma用于存放念頭奶牛之間是否有關聯,因為battle有勝負之分,所以關聯是單向的。在Floyd算法中只需要判斷兩點之間是否有單項的關聯,最后判斷名次時,要明白
一頭奶牛在整體中的排名能被確定?<=>?它能被x頭不同的奶牛直接或間接打敗,同時也能直接或間接打敗y頭奶牛,也就是它與其他奶牛有雙向的關聯。
代碼:
1 #include <cstdio> 2 #include <fstream> 3 #include <algorithm> 4 #include <cmath> 5 #include <deque> 6 #include <vector> 7 #include <queue> 8 #include <string> 9 #include <cstring> 10 #include <map> 11 #include <stack> 12 #include <set> 13 #include <sstream> 14 #include <iostream> 15 #define mod 998244353 16 #define eps 1e-6 17 #define ll long long 18 #define INF 0x3f3f3f3f 19 using namespace std; 20 21 int main() 22 { 23 //n頭奶牛,m場對決 24 int n,m; 25 //ma用于存放兩頭奶牛之間是否有關系。 26 int ma[110][110]; 27 scanf("%d %d",&n,&m); 28 //初始化為0表示都沒有關系 29 memset(ma,0,sizeof(ma)); 30 int a,b; 31 for(int i=1;i<=m;i++) 32 { 33 scanf("%d %d",&a,&b); 34 //為1表示a和b之間有關系,而且是a到b單向的關系 35 ma[a][b]=1; 36 } 37 //Floyd算法核心 38 for(int k=1;k<=n;k++) 39 { 40 for(int i=1;i<=n;i++) 41 { 42 for(int j=1;j<=n;j++) 43 { 44 //如果i到k有關系,并且k帶j有關系,則i可以與j有關系 45 if(ma[i][k]&&ma[k][j]) 46 { 47 ma[i][j]=1; 48 } 49 } 50 } 51 } 52 //判斷排位名次 53 int ans=0; 54 for(int i=1;i<=n;i++) 55 { 56 int num=1; 57 for(int j=1;j<=n;j++) 58 { 59 //如果i能到j,或j能到i,則兩者之間有關系 60 if(ma[i][j]||ma[j][i]) 61 { 62 num++; 63 } 64 } 65 //如果i與其他奶牛都有關系,則可以判斷i的名次 66 if(num==n) 67 { 68 ans++; 69 } 70 } 71 printf("%d\n",ans); 72 }
?
?轉載于:https://www.cnblogs.com/mzchuan/p/11490020.html
總結
以上是生活随笔為你收集整理的Cow Contest POJ - 3660 Floyd算法,关系链图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二进制介绍、编码、计算机数据计量单位
- 下一篇: RT6863D/TR(6863D)