程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> [HOJ]HIT's Powerstation

[HOJ]HIT's Powerstation

編輯:C++入門知識

這個題沒什麼復雜的地方,就是一個關於最短路徑的實現,可以用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

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 
#include 
#include 
#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<>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< minmal[j]){
                m = minmal[j];
                n = j;
            }
        }
        cout<

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved