POJ1975 Median Weight Bead
2012-08-17 21:20
411 查看
Median Weight Bead
Description
There are N beads which of the same shape and size, but with different weights. N is an odd number and the beads are labeled as 1, 2, ..., N. Your task is to find the bead whose weight is median (the ((N+1)/2)th among all beads). The following comparison has been performed on some pairs of beads:
A scale is given to compare the weights of beads. We can determine
which one is heavier than the other between two beads. As the result, we
now know that some beads are heavier than others. We are going to
remove some beads which cannot have the medium weight.
For example, the following results show which bead is heavier after M comparisons where M=4 and N=5.
From the above results, though we cannot determine exactly which is
the median bead, we know that Bead 1 and Bead 4 can never have the
median weight: Beads 2, 4, 5 are heavier than Bead 1, and Beads 1, 2, 3
are lighter than Bead 4. Therefore, we can remove these two beads.
Write a program to count the number of beads which cannot have the median weight.
Input
The
first line of the input file contains a single integer t (1 <= t
<= 11), the number of test cases, followed by the input data for each
test case. The input for each test case will be as follows:
The first line of input data contains an integer N (1 <= N <=
99) denoting the number of beads, and M denoting the number of pairs of
beads compared. In each of the next M lines, two numbers are given where
the first bead is heavier than the second bead.
Output
There should be one line per test case. Print the number of beads which can never have the medium weight.
Sample Input
Sample Output
Source
Tehran Sharif 2004 Preliminary
思路:题目给出N个BEADS,每个BEADS都有一个权值,将第(N+1)/2重的权值定义为中间权值。然后给出M组数据对如:a b,表示a的权值大于b的权值。依据这些可以排除一些BEADS肯定不可能是中间权值的BEADS。N为BEAD是的数目吗,且N为奇数。
根据题意可以把数据对a,b表示成点a到点b的距离为1(是有向边),然后没有边相连的用MAXINT表示。最后用FLOYD就可以求出没对点之间的最短路径,只要最短路径小于MAXINT就说明可以确定这两个点之间的大小关系。再建立邻接矩阵的同时,建立反连接矩阵,同样使用FLOYD。依据矩阵1就可以求出比点I大的点的个数,依据矩阵2就可以求出比点I小的点个数。然后如果比I大或者小的点的个数超过了或者等于(N+1)/2,则表示点I不可能是中间权值的BEADS。
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 2356 | Accepted: 1182 |
There are N beads which of the same shape and size, but with different weights. N is an odd number and the beads are labeled as 1, 2, ..., N. Your task is to find the bead whose weight is median (the ((N+1)/2)th among all beads). The following comparison has been performed on some pairs of beads:
A scale is given to compare the weights of beads. We can determine
which one is heavier than the other between two beads. As the result, we
now know that some beads are heavier than others. We are going to
remove some beads which cannot have the medium weight.
For example, the following results show which bead is heavier after M comparisons where M=4 and N=5.
1. Bead 2 is heavier than Bead 1. 2. Bead 4 is heavier than Bead 3. 3. Bead 5 is heavier than Bead 1. 4. Bead 4 is heavier than Bead 2.
From the above results, though we cannot determine exactly which is
the median bead, we know that Bead 1 and Bead 4 can never have the
median weight: Beads 2, 4, 5 are heavier than Bead 1, and Beads 1, 2, 3
are lighter than Bead 4. Therefore, we can remove these two beads.
Write a program to count the number of beads which cannot have the median weight.
Input
The
first line of the input file contains a single integer t (1 <= t
<= 11), the number of test cases, followed by the input data for each
test case. The input for each test case will be as follows:
The first line of input data contains an integer N (1 <= N <=
99) denoting the number of beads, and M denoting the number of pairs of
beads compared. In each of the next M lines, two numbers are given where
the first bead is heavier than the second bead.
Output
There should be one line per test case. Print the number of beads which can never have the medium weight.
Sample Input
1 5 4 2 1 4 3 5 1 4 2
Sample Output
2
Source
Tehran Sharif 2004 Preliminary
思路:题目给出N个BEADS,每个BEADS都有一个权值,将第(N+1)/2重的权值定义为中间权值。然后给出M组数据对如:a b,表示a的权值大于b的权值。依据这些可以排除一些BEADS肯定不可能是中间权值的BEADS。N为BEAD是的数目吗,且N为奇数。
根据题意可以把数据对a,b表示成点a到点b的距离为1(是有向边),然后没有边相连的用MAXINT表示。最后用FLOYD就可以求出没对点之间的最短路径,只要最短路径小于MAXINT就说明可以确定这两个点之间的大小关系。再建立邻接矩阵的同时,建立反连接矩阵,同样使用FLOYD。依据矩阵1就可以求出比点I大的点的个数,依据矩阵2就可以求出比点I小的点个数。然后如果比I大或者小的点的个数超过了或者等于(N+1)/2,则表示点I不可能是中间权值的BEADS。
#include <cstdlib> #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <string> #define MAXINT 99999999 using namespace std; int main(int argc, char *argv[]) { int t; int i,j,k; int n,m; int data[100][100]; int data2[100][100]; int ncount=0; int isVis[100]; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); ncount=0; for(i=1;i<=n;i++) for(j=1;j<=n;j++) data2[i][j]=data[i][j]=MAXINT; for(i=1;i<=n;i++) isVis[i]=0; for(i=0;i<m;i++) { int a,b; scanf("%d%d",&a,&b); data[a][b]=1; data2[b][a]=1; } for(k=1;k<=n;k++) for(i=1;i<=n;i++) for(j=1;j<=n;j++) { if(data[i][j]>data[i][k]+data[k][j]) data[i][j]=data[i][k]+data[k][j]; if(data2[i][j]>data2[i][k]+data2[k][j]) data2[i][j]=data2[i][k]+data2[k][j]; } int midNum=(1+n)/2; for(i=1;i<=n;i++) { int tmpNum=0; for(j=1;j<=n;j++) { if(data[i][j]<MAXINT) tmpNum++; } if((tmpNum>=midNum)&&(isVis[i]==0)) {ncount++;isVis[i]=1;} } for(i=1;i<=n;i++) { int tmpNum=0; for(j=1;j<=n;j++) { if(data2[i][j]<MAXINT) tmpNum++; } if((tmpNum>=midNum)&&(isVis[i]==0)) {ncount++;isVis[i]=1;} } printf("%d\n",ncount); } //system("PAUSE"); return EXIT_SUCCESS; }
相关文章推荐
- POJ1975 Median Weight Bead [Floyd]
- POJ1975:Median Weight Bead(FLOYD)
- poj1975 Median Weight Bead ----floyd 传递闭包
- poj1975 Median Weight Bead(floyd || 暴搜)
- ZOJ 2500 Median Weight Bead
- POJ 1975:Median Weight Bead
- zoj 2500 || poj 1975 Median Weight Bead
- hoj 2218 Median Weight Bead // poj 1975 Median Weight Bead 有向图的传递闭包问题
- POJ 1975 Median Weight Bead floyd求传递闭包 || bfs
- POJ 1975 Median Weight Bead
- poj 1975 Median Weight Bead
- OpenJudge 1975 Median Weight Bead
- POJ-1975 Median Weight Bead(Floyed)
- POJ-1975 Median Weight Bead(Floyed)
- 1975(Median Weight Bead)
- poj 1975 Median Weight Bead(传递闭包 Floyd)
- POJ1975 Median Weight Bead
- POJ 1975 Median Weight Bead(Floyd传递闭包)
- poj 1975 Median Weight Bead 传递闭包的应用
- poj&nbsp;1975&nbsp;Median&nbsp;Weight&nbsp;Bead(floy…