ZOJ 3708 Density of Power Network(水题,图论)

来源:互联网 发布:java简单源码 编辑:程序博客网 时间:2024/05/22 02:23

The vast power system is the most complicated man-made system and the greatest engineering innovation in the 20th century. The following diagram shows a typical 14 bus power system. In real world, the power system may contains hundreds of buses and thousands of transmission lines.

此处缺少一个图,请大家去原网站去看。
网址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3708
Network topology analysis had long been a hot topic in the research of power system. And network density is one key index representing the robustness of power system. And you are asked to implement a procedure to calculate the network density of power system.

The network density is defined as the ratio between number of transmission lines and the number of buses. Please note that if two or more transmission lines connecting the same pair of buses, only one would be counted in the topology analysis.

Input

The first line contains a single integer T (T ≤ 1000), indicating there are T cases in total.

Each case begins with two integers N and M (2 ≤ N, M ≤ 500) in the first line, representing the number of buses and the number of transmission lines in the power system. Each Bus would be numbered from 1 to N.

The second line contains the list of start bus number of the transmission lines, separated by spaces.

The third line contains the list of corresponding end bus number of the transmission lines, separated by spaces. The end bus number of the transmission lines would not be the same as the start bus number.

Output

Output the network density of the power system in a single line, as defined in above. The answer should round to 3 digits after decimal point.

Sample Input

3
3 2
1 2
2 3
2 2
1 2
2 1
14 20
2 5 3 4 5 4 5 7 9 6 11 12 13 8 9 10 14 11 13 13
1 1 2 2 2 3 4 4 4 5 6 6 6 7 7 9 9 10 12 14
Sample Output

0.667
0.500
1.429

本题题意:
给出车的数目,和每个路线的起点和终点。让你求出路线的数目与车的数目的比。如果两条路线的起点和终点都相同,那么这两条路线算一条路线。如果一条路线的终点和另一条路线的起点相同并且这条路线的起点和另一条路线的终点相同,那他们算同一条路线,接下来求出比值。

下面附上AC代码:

#include<cstdio>#include<cstring>#include<cmath>#include<queue>#include<stack>#include<algorithm>using namespace std;int a[505][505],b[505],c[505];int main(){    int t,n,m;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        for(int i=1;i<=m;i++)        {            scanf("%d",&b[i]);        }        for(int i=1;i<=m;i++)        {            scanf("%d",&c[i]);        }        memset(a,0,sizeof(a));        for(int i=1;i<=m;i++)        {            a[b[i]][c[i]]++;            a[c[i]][b[i]]++;        }        double sum=0;        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)            {                if(a[i][j]!=0)                {                    sum=sum+1;                }            }        }        sum=sum/2;        printf("%.3lf\n",sum/(n*1.0));    }    return 0;}
0 0
原创粉丝点击