[HOJ]HIT's Powerstation

这个题没什么复杂的地方,就是一个关于最短路径的实现,可以用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

1
9 11
0 1 1
1 2 3
0 3 4
0 4 7
2 4 6
3 6 3
3 7 8
2 7 9
2 5 2
5 8 5
7 8 3

#include <iostream>
#include <cstring>
#include <cstdlib>
#define MAX 100
#define MAXA 1000000
using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值