[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; }}
- [HOJ]HIT's Powerstation
- hoj Set S
- HOJ 2017 Peter's smokes
- HOJ 1459 Goldbach's Conjecture
- HOJ 1016 Joseph's problem I&&HOJ 2920 Escape
- HOJ 2067 Adding 1s, 2s, and 3s
- HOJ 3087 Discover's Problem I
- HOJ 3057 Cai's DOTA game
- HOJ 2782 Alfredo's Pizza Restaurant
- HOJ 1271 Biker's Trip Odometer
- HOJ 1016 Joseph's problem I
- HOJ 1019 Grandpa's Other Estate
- Hoj 3134 Xiaodai’s Budget Program
- HOJ 1017 Joseff's problem II
- (简单) 博弈 HOJ 2128 S-Nim
- 【hoj】1016 Joseph's problem I
- 【hoj】 1017 Joseph's problem II
- HOJ 12879 The Queen's English
- C# Excel导入数据到数据库
- UVa 216 Getting in Line
- jquery判断小数点两位跟自动删除小数两位后的数字
- 开源许可证
- POJ 1228 凸包惟一性判断(多边形模板)
- [HOJ]HIT's Powerstation
- 并查集找gangs
- OCM_session0手动建库实验
- 安装使用RGBDSLAM_Freiburg
- Best of Vim Tips 中文版
- css和js学习的体会
- 我们什么时候应该使用动态内存
- 一个新的开始
- php连接mysql数据库