您的位置:首页 > 其它

POJ1975 Median Weight Bead

2012-08-17 21:20 411 查看
Median Weight Bead

Time Limit: 1000MSMemory Limit: 30000K
Total Submissions: 2356Accepted: 1182
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.

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: