ACM刷題之ZOJ————Density of Power Network

Density of Power Network

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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.

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 ≤ NM ≤ 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<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<iostream>
using namespace std;
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int N=2e5+7;

typedef struct nd{
	int x,y;
}nd ;

nd s[20000];

int d1[20000];
int d2[20000];

int main()
{
	//freopen("f:/input.txt", "r", stdin);
	int zu,i,j,k,m,cnt,f;
	double x1,y1,nk,kk,n;
	nd a;
	scanf("%d",&zu);
	while(zu--)
	{
		CLR(d1,0);
		CLR(d2,0);
		CLR(s,0);
		scanf("%lf%d",&n,&m);
		cnt=0;
		for(i=0;i<m;i++)
		{
			scanf("%d",&d1[i]);
		}
		for(i=0;i<m;i++)
		{
			scanf("%d",&d2[i]);
			if(d1[i]>d2[i])
			{
				k=d1[i];
				d1[i]=d2[i];
				d2[i]=k;
			}
		}
		cnt=0;
		kk=0;
		for(i=0;i<m;i++)
		{
			f=0;
			
			for(j=0;j<cnt;j++)
			{
				
				if(d1[i]==s[j].x&&d2[i]==s[j].y)
				{
					f=1;
					break;
				}
			}
			if(f==0)
			{
				s[cnt].x=d1[i];
				s[cnt].y=d2[i];
				kk=kk+1.0; 
				++cnt;
			}
		}
		
		//nk=(double)n;
		printf("%.3lf\n",kk/n);
		
	}
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章