HDU 1102 Constructing Roads

 

http://acm.hdu.edu.cn/showproblem.php?pid=1102

 

题目大意:又是求最短路径的问题啦,只不过有些路已经建好了,现在要你计算连通所有点,还要花修路费,建好的就不用考虑了。

 

解题思路:已经做到麻木的类型题了,不说了。

 

#include <stdio.h> #include <string.h> #define MaxSize 105 #define INIT 999999999 int Graph[MaxSize][MaxSize]; long sum; bool visited[MaxSize]; int NumOfVillage; void Prim() { int i,j; sum = 0; int dist[MaxSize]; int min,locate; memset(visited,0,sizeof(visited)); for(i=1;i<=NumOfVillage;i++) dist[i] = Graph[1][i]; visited[1] = true; for (j=1;j<=NumOfVillage;j++) { min = INIT; for (i=1;i<=NumOfVillage;i++) { if(!visited[i]&&dist[i]<min) { min = dist[i]; locate = i; } } visited[locate] = true; for (i=1;i<=NumOfVillage;i++) { if (!visited[i]&&dist[i]>Graph[locate][i]) { dist[i] = Graph[locate][i]; } } } for (i=1;i<=NumOfVillage;i++) { sum+=dist[i]; } } int main() { int i,j,Q; while (scanf("%d",&NumOfVillage)!=EOF) { for (i=1;i<=NumOfVillage;i++) { for(j=1;j<=NumOfVillage;j++) scanf("%d",&Graph[i][j]); } scanf("%d",&Q); while (Q--) { scanf("%d%d",&i,&j); Graph[i][j] = 0; Graph[j][i] = 0; } Prim(); printf("%ld/n",sum); } return 0; }

你可能感兴趣的:(HDU 1102 Constructing Roads)