[HOJ]HIT's Powerstation

来源:互联网 发布:m2同比增速 数据 编辑:程序博客网 时间:2024/05/16 13:54

这个题没什么复杂的地方,就是一个关于最短路径的实现,可以用floyd算法,也可以用Dijkstra算法实现,下面是用Dijsktra算法实现的

题目:

Since the satellite of HIT has been sent to space successfully, HIT needs more electric power. Shuguo Wang has decided to build a new powerstation in HIT.

e.g. The graph of HIT is: 
""" where vertexes denotes electricity consumers, and edges denotes wires with their length between consumers. The powerstation will locate at one vertex, and the loss is the sum of the shortest paths from other vertexes to the powerstation. Can you calculate the minimal loss of HIT's graph?

Input
There are multiple test cases.(We can be Lei Feng to help other brother universities) The first line, an integer n(0<n<20), is the number of the test cases. In each test case, an integer v(1<v<=100) shows the number of vertexes in a single line, and an integer e is the number of edges. then followed by e lines. Each line contains 3 integers: one vertex number, the other vertex number, and the length of the wire.Vertex number is from 0 to v-1.

Output
For each test case, output the powerstation's location(the vertex number) and the minimal loss seperated by a space character in a line. Choose the smallest vertex number if there are more than 1 locations.

Sample Input

19 110 1 11 2 30 3 40 4 72 4 63 6 33 7 82 7 92 5 25 8 57 8 3

#include <iostream>#include <cstring>#include <cstdlib>#define MAX 100#define MAXA 1000000using namespace std;int sort(int D[],int v){    int m,n;    m = D[0],n = 0;    for(int i = 0;i < v;i++){        if(D[i] < m){            m = D[i];            n = i;        }    }    return n;}int shortPath(int graph[][MAX],int k,int v){    int D[MAX];//表示从源点到顶点i的当前最短路径    //int P[MAX];//表示源点到i路径经过的最后的节点    bool S[MAX];//存放原点和已生成的终点    int a,temp;    int pathLen = 0;    memset(S,false,sizeof(S));    S[k] = true;    for(int i = 0;i < v;i++)         D[i] = graph[k][i];    D[k] = MAXA;    for(int j = 0;j < v-1;j++){         a = sort(D,v);//表示找到当前路径中最短的路        pathLen += D[a];       // cout<<a<<' '<<D[a]<<endl;        for(int n = 0;n < v;n++){            temp = D[a] + graph[a][n];            if((temp < D[n]) && (!S[n]))                 D[n] = temp;        }         D[a] = MAXA;         S[a] = true;    }    return pathLen;}int main(){    int n,v,e;    int a,b,c;    int graph[MAX][MAX];    int minmal[MAX];//记录以每个顶点作为源点的顶点号和最短路径    cin>>n;    for(int i = 0;i < n;i++){        cin>>v;//顶点数        cin>>e;//边数        for(int n = 0;n < v;n++){            for(int m = 0;m < v;m++)                graph[n][m] = MAXA;        }        for(int j = 0;j < e;j++){            cin>>a;            cin>>b;            cin>>c;            graph[a][b] = c;            graph[b][a] = c;        }        for(int k = 0;k < v;k++){//分别以每个顶点作为源点            minmal[k] = shortPath(graph,k,v);          //  cout<<k<<' '<<minmal[k]<<endl;        }        int m,n;        m = minmal[0];        n = 0;        for(int j = 0;j < v;j++){            if(m > minmal[j]){                m = minmal[j];                n = j;            }        }        cout<<n<<" "<<m<<endl;    }}


0 0
原创粉丝点击