D - Density of Power Network ZOJ - 3708

最近感觉刷题思路很不严谨!!,

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
题意就是t组数据,每组输入n,m代表n个车,m条线路,如果两条线路起点与起点相同终点与终点相同或着起点与终点相同终点与起点相同则这两条路相同!求不同的路的数目与n的比值结果。保留三位小数.

#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
int a[1001],b[1001];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=m;i++)
        {
            cin>>a[i];
        }
        for(int i=1;i<=m;i++)
        {
            cin>>b[i];
        }
        int sum=0;
        for(int i=1;i<=m;i++)
        {
            if(a[i]==-1&&b[i]==-1)
            {
                continue;
            }
            for(int j=i+1;j<=m;j++)
            {
                if(a[j]==-1&&b[j]==-1)
                {
                    continue;
                }
                if(a[i]==a[j]&&b[i]==b[j]||a[i]==b[j]&&b[i]==a[j])
                {
                    sum++;
                   // a[i]=-1;//我去错了几遍居然是这里我给赋值了!赋值了还怎么继续比较啊!!晕菜!!
                   // b[i]=-1;
                    a[j]=-1;
                    b[j]=-1;
                }
            }

        }
        double p=(m-sum)*1.000/n;
        printf("%.3f\n",p);
    }
    return 0;

}
这是自己的思路,开始居然把a[i]b[i]赋值-1了!赋值完了还怎么比较啊!晕菜。

后来看了人家的代码不必这么复杂,与我类似的思路有。还有直接建立图的

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
int main(){
    int t;
    cin >> t;
    while(t--)
    {
        int n,m;
        cin >> n >>m;
        int a[2000];
        int b[2000];
        for (int i = 0; i < m; i++){
            cin >> a[i];
        }
        for (int i = 0; i < m; i++){
            cin >> b[i];
        }
        int cnt = 0;
        for (int i = 0; i < m - 1; i++){
            for (int j = i + 1; j < m; j++){
                if (a[i] == b[j] && b[i] == a[j] )
                {
                    cnt++;
                    break;
                }
                else if(a[i] == a[j] && b[i] == b[j]  )
                {
                    cnt++;
                    break;
                }
            }
        }
        double ans;
        if (cnt == 0)
            ans = m * 1.0 / n ;
        else
            ans = (m - cnt) * 1.0 / n ;
        printf("%.3f\n",ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值