HDU -1704 Rank——floyd
🚪:Rank
題意:
判斷都多少人無(wú)法確定關(guān)系
題解:
首先可以將題轉(zhuǎn)換成一個(gè)最短路問(wèn)題,將題目給出 的關(guān)系距離為0,沒(méi)給的距離為0x3f,(單向)走一遍Floyd算法,最后判斷。距離是0x3f就將答案+1(因?yàn)樗麄z沒(méi)有關(guān)系),最后輸出。
錯(cuò)點(diǎn)
剛開(kāi)始建立的是單向圖,題中也說(shuō)不會(huì)重復(fù)詢問(wèn),最后判斷時(shí)判斷的是上三角,錯(cuò)了6次
最后找了一組數(shù)據(jù),
4 2
3 2
4 2
這組數(shù)據(jù)答案應(yīng)為4,而我的是6,因?yàn)?#xff0c;判斷上三角是判斷的是
1->2 1->3 1->4
2->3 2->4
3->4
又由于數(shù)據(jù)中是單向的且是3->2 4->2。這是在上三角中是不會(huì)存在的,所以最后判斷時(shí)
f(a[j][i]==0x3f3f3f3f&&a[i][j]==0x3f3f3f3f)c++;這樣判斷才行。
由于數(shù)據(jù)太大,Floyd需要優(yōu)化一下
code
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<queue> #include<stack> #include<set> #include<map> #define x first #define y second #define met(a,b) memset(a,b,sizeof a) #define open ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); using namespace std; map<string,int>mapp; typedef long long ll; const int N=2500+10; int n,m,s; int a[505][505]; void floyd() {for(int k=1; k<=n; k++) {for(int i=1; i<=n; i++) {if(a[i][k]!=0x3f3f3f3f) {//優(yōu)化for(int j=1; j<=n; j++) {a[i][j]=min(a[i][j],a[i][k]+a[k][j]);}}}} } int main() {int t;while(cin>>t)while(t--) {met(a,0x3f);scanf("%d%d",&n,&m);for(int i=1; i<=n; i++) {a[i][i]=0;}while(m--) {int x,y;scanf("%d%d",&x,&y);int xx,yy;a[x][y]=0;}floyd();int c=0;for(int i=1; i<=n; i++) {for(int j=i+1; j<=n; j++) {if(a[j][i]==0x3f3f3f3f&&a[i][j]==0x3f3f3f3f)c++;}}printf("%d\n",c);} }Problem Description
there are N ACMers in HDU team.
ZJPCPC Sunny Cup 2007 is coming, and lcy want to select some excellent ACMers to attend the contest. There have been M matches since the last few days(No two ACMers will meet each other at two matches, means between two ACMers there will be at most one match). lcy also asks"Who is the winner between A and B?" But sometimes you can’t answer lcy’s query, for example, there are 3 people, named A, B, C.and 1 match was held between A and B, in the match A is the winner, then if lcy asks “Who is the winner between A and B”, of course you can answer “A”, but if lcy ask “Who is the winner between A and C”, you can’t tell him the answer.
As lcy’s assistant, you want to know how many queries at most you can’t tell lcy(ask A B, and ask B A is the same; and lcy won’t ask the same question twice).
Input
The input contains multiple test cases.
The first line has one integer,represent the number of test cases.
Each case first contains two integers N and M(N , M <= 500), N is the number of ACMers in HDU team, and M is the number of matchs have been held.The following M lines, each line means a match and it contains two integers A and B, means A wins the match between A and B.And we define that if A wins B, and B wins C, then A wins C.
Output
For each test case, output a integer which represent the max possible number of queries that you can’t tell lcy.
Sample Input
3
3 3
1 2
1 3
2 3
3 2
1 2
2 3
4 2
1 2
3 4
Sample Output
0
0
4
總結(jié)
以上是生活随笔為你收集整理的HDU -1704 Rank——floyd的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 生物仿生细胞膜包裹药物|人源肝癌细胞He
- 下一篇: layui card高度不匹配文字