代码改变世界

Work Conversion

2015-05-27 12:05  kingshow  阅读(328)  评论(0编辑  收藏  举报

Most work in a factory is performed by robots. In the robots’ work, the most time used is when converting an operation.
Therefore, if you design an automation system, you need to consider the work conversion.
Consider the work conversion status as given in the graph below:

 

 

 

 

For the above case,
it is 1, 1, 2 for the shortest frequency of work conversion from Work No. 1 to Work No. 2, 3 and 4;
it is 3, 2, 1 from Work No. 2 to No. 1, 3 and 4; it is 1, 2, 3 for Work No. 3 to No. 1, 2 and 4;
and it is 2, 3, 1 from Work No. 4 to No. 1, 2 and 3.
At this moment, the sum of the shortest frequencies of the work conversions is 1+1+2+3+2+1+1+2+3+2+3+1 = 22.


In this graph, all pairs are 12 pairs i.e., from Work No. 1 to No. 2; from No. 1 to No. 3; ...; from No. 4 to No. 3.
Therefore, the mean frequency of conversion from a certain work to another is 22/12, which is 1.833.


Calculate the mean frequency of work conversions from a certain work to another when a graph of the work conversion status is given.


Time limit : 1 sec (Java : 2 sec)


[Input]
There can be more than one test case in the input file. The first line has T, the number of test cases.
Then the totally T test cases are provided in the following lines (T ≤ 10 )


The count of work Conversions, N, is given at the first row. (1 ≤ N ≤ 500)
The moving information, s and e, is given from the next row to the number of rows of N, which means it can move from Work No. s to Work No. e.
However, it can’t go from Work No. e to Work No. s directly as the work movement processes only one way in serial order.
At this moment, there are no such cases that are unable to move from a certain work no. to another.


[Output]
For each test case, you should print "Case #T" in the first line where T means the case number.

For each test case, you should round off the mean frequency of work conversion at three decimal places moving from a certain work to another at the first row and generate the values.


[I/O Example]

Input
2
5
1 2
2 4
1 3
3 1
4 3
25
1 3
1 5
1 7
2 1
2 7
3 8
3 9
3 10
4 7
5 1
6 4
6 7
7 10
7 11
8 1
8 4
8 5
8 10
9 10
10 1
10 2
10 6
10 8
10 9
11 3


Output
Case #1

1.833

Case #2
2.255 

代码:

#include <iostream>
#include <stdio.h>
#include <queue>
#include <iomanip>

#define MAX 9999
int map[501][501];
int dis[501];
int sum[501];

int InitArray();
using namespace std;

int main()
{
    //freopen("test.txt","r",stdin);
    int T;
    cin>>T;
    for(int t=1; t<=T; t++)
    {
        int n;
        int s,e;
        int maxPoint = 0;
        int temp = 0;
        int num;
        float countSteps = 0;
        float countPairs = 0;
        float result = 0;
        queue<int> qt;
        InitArray();
        cin>>n;
        for(int i=0; i<n; i++)
        {
            cin>>s>>e;
            map[s][e] = 1;
            if(s >= maxPoint)
            {
                maxPoint = s;
            }
            else if(e >= maxPoint)
            {
                maxPoint = e;
            }
        }

        for(int k=1; k<=maxPoint; k++)
        {
            for(int i=1; i<=maxPoint; i++)
            {
                dis[i] = MAX;
            }
            dis[k] = 0;
            qt.push(k);

            while(!qt.empty())
            {
                num = qt.front();
                qt.pop();
                for(int h=1; h<=maxPoint; h++)
                {
                    temp = dis[num] + map[num][h];
                    if(temp < dis[h])
                    {
                        dis[h] = temp;
                        qt.push(h);
                    }
                }
            }

            for(int i=1; i<=maxPoint; i++)
            {
                if(dis[i]>0 && dis[i]< MAX)
                {
                    countPairs++;
                    sum[k] += dis[i];
                }
            }
        }

        for(int j=1; j<=maxPoint; j++)
        {
            countSteps += sum[j];
        }

        result = countSteps/countPairs;
        cout<<"Case #"<<t<<endl;
        //cout<<result<<endl;
        //cout<<fixed<<setprecision(3)<<result<<endl;
        printf("%.3f\n",result);
    }


    //cout << "Hello world!" << endl;
    return 0;
}

int InitArray()
{
    for(int i=1; i<501; i++)
    {
        dis[i] = MAX;
        sum[i] = 0;
        for(int j=1; j<501; j++)
        {
            if(i == j)
            {
                map[i][j] = 0;
            }
            else
            {
                map[i][j] = MAX;
            }
        }
    }
    return 0;
}
View Code