标签:
3 1 2 1 0 1 3 2 0 2 3 4 0 3 1 2 1 0 1 3 2 0 2 3 4 1 3 1 2 1 0 1 3 2 1 2 3 4 1 0
3 1 0
该题在1024题的基础上略微修改即可,已修的路价格为0,未修的路价格为原价
代码如下:
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <string> 5 #define MAX 102 6 #define inf 0x3f3f3f3f 7 int flag[MAX]; 8 int cost[MAX][MAX]; 9 int lowCost[MAX]; 10 11 int main(int argc, char const *argv[]) 12 { 13 int n,m; 14 scanf("%d",&n); 15 while(n != 0) { 16 int count = 0; 17 m = n * (n-1)/2; 18 for(int i = 1; i <= n; i++) { 19 flag[i] = 0; 20 for(int j = 1; j <= n; j++) { 21 cost[i][j] = inf; 22 } 23 } 24 for(int i = 0; i < m; i++) { 25 int a,b,c,d; 26 scanf("%d %d %d %d",&a,&b,&c,&d); 27 if(d == 0) { 28 cost[a][b]= cost[b][a] = c; 29 } 30 else { 31 cost[a][b]= cost[b][a] = 0; 32 } 33 } 34 35 int sumCost = 0; 36 for(int i = 1; i <= n; i++) { 37 lowCost[i] = cost[1][i]; 38 } 39 flag[1] = 1; 40 41 for(int i = 1; i <= n; i++) { 42 int min = inf; 43 int v = -1; 44 for(int i = 1; i <= n; i++) { 45 if(flag[i] == 0 && lowCost[i] < min) { 46 min = lowCost[i]; 47 v = i; 48 } 49 } 50 flag[v] = 1; 51 sumCost = sumCost + lowCost[v]; 52 53 for(int i = 1; i <= n; i++) { 54 if(cost[v][i] < lowCost[i]) { 55 lowCost[i] = cost[v][i]; 56 } 57 } 58 } 59 60 printf("%d\n",sumCost); 61 62 scanf("%d",&n); 63 } 64 65 return 0; 66 }
标签:
原文地址:http://www.cnblogs.com/jasonJie/p/5677484.html