URAL 1416 Confidential
来源:互联网 发布:python 邮件服务器 编辑:程序博客网 时间:2024/06/11 20:59
#include <stdio.h>#define INF (1 << 31 - 1)#define MAX_PLANETS 501int numOfPlanets;int numOfPassages;int costArray[MAX_PLANETS][MAX_PLANETS];int minCostArray[MAX_PLANETS];int added[MAX_PLANETS];int planetAddBy[MAX_PLANETS];int totalCost;int stack[MAX_PLANETS];int top;int maxCostArray[MAX_PLANETS][MAX_PLANETS];//maxCostArray[i][j]来表示MST中从i到j经过的最大边权 PS:中途可能经过多个边int noMST;int main(){//freopen("input.txt", "r", stdin);/* input */scanf("%d %d", &numOfPlanets, &numOfPassages);//reset int from, to;for (from = 1; from <= numOfPlanets; from++){minCostArray[from] = INF;planetAddBy[from] = 1;for (to = from + 1; to <= numOfPlanets; to++){costArray[from][to] = INF;costArray[to][from] = INF;}}int indexOfPassage;for (indexOfPassage = 1; indexOfPassage <= numOfPassages; indexOfPassage++){int from, to, cost;scanf("%d %d %d", &from, &to, &cost);costArray[from][to] = cost;costArray[to][from] = cost;}/* minimum spanning tree Prim algorithm *///initial minCostrArrayint indexOfPlanet;for (indexOfPlanet = 2; indexOfPlanet <= numOfPlanets; indexOfPlanet++){minCostArray[indexOfPlanet] = costArray[1][indexOfPlanet];}minCostArray[1] = 0;added[1] = 1;top++;stack[top] = 1;//main loopint numOfPlanetsAdded;for (numOfPlanetsAdded = 2; numOfPlanetsAdded <= numOfPlanets; numOfPlanetsAdded++){//to find min cost in minCostrArrayint minCost = INF;int minCostPlanet = 1;for (indexOfPlanet = 2; indexOfPlanet <= numOfPlanets; indexOfPlanet++){if (added[indexOfPlanet] == 0 && minCostArray[indexOfPlanet] < minCost){minCost = minCostArray[indexOfPlanet];minCostPlanet = indexOfPlanet;}}if (minCost == INF){noMST = 1;break;}//add minCostPlanet into MSTadded[minCostPlanet] = 1;totalCost += minCost;top++;stack[top] = minCostPlanet;//update maxCostArrayint index;for (index = 1; index < top; index++){int planetAdded = stack[index];int maxCost = maxCostArray[ planetAddBy[minCostPlanet] ] [planetAdded];if (minCost > maxCost){maxCostArray[minCostPlanet][planetAdded] = minCost;maxCostArray[planetAdded][minCostPlanet] = minCost;} else {maxCostArray[minCostPlanet][planetAdded] = maxCost;maxCostArray[planetAdded][minCostPlanet] = maxCost;}}//update minCostrArrayfor (indexOfPlanet = 2; indexOfPlanet <= numOfPlanets; indexOfPlanet++){if (added[indexOfPlanet] == 0 && costArray[indexOfPlanet][minCostPlanet] < minCostArray[indexOfPlanet]){minCostArray[indexOfPlanet] = costArray[indexOfPlanet][minCostPlanet];planetAddBy[indexOfPlanet] = minCostPlanet;//记录indexOfPlanet是通过minCostPlanet跟新与MST的距离}}}/* output */if (noMST){printf("Cost: -1\nCost: -1");return 0;}printf("Cost: %d\n", totalCost);int nextTotalCost = INF;for (from = 1; from <= numOfPlanets; from++){for (to = from + 1; to <= numOfPlanets; to++){if (costArray[from][to] != INF && planetAddBy[from] != to && planetAddBy[to] != from){//加入一条不在MST中的边costArray[from][to],减去maxCostArray[from][to]int newTotalCost = totalCost + costArray[from][to] - maxCostArray[from][to];if (newTotalCost < nextTotalCost){nextTotalCost = newTotalCost;}}}}if (nextTotalCost == INF){printf("Cost: -1");} else {printf("Cost: %d", nextTotalCost);}return 0;}
0 0
- ural 1416 Confidential
- URAL 1416 Confidential
- URAL 1416 Confidential
- Ural 1416 Confidential,次小生成树
- URAL - 1416 Confidential(次小生成树)
- URAl 1416Confidential【次小生成树】
- ural 1416. Confidential
- URAL 1416 Confidential <最小生成数和次小生成数>
- URAL 1416 Confidential --最小生成树与次小生成树
- Ural 1416 Confidential(最小生成树+次小生成树)
- poj1679 The Unique MST(次小生成树) Ural 1416 Confidential
- 薪资报价(Confidential)
- URAL
- 【ural】
- URAL
- URAL
- URAL
- URAL
- javascript预加载图片的处理(src无法加载图片)
- caffe中的卷积实现
- jenkins 忘记admin用户账号密码
- Android开发---把int转换为String出现的问题
- stm32 IAP + APP ==>双剑合一
- URAL 1416 Confidential
- android中的Parcelable接口
- Struts2中的拦截器
- JS对字符串的处理总结
- iOS之UITableView
- OpenStack: Perform Consistent Snapshots
- 跳转场景的3种方式
- oc-分类(catgory)的概念及使用
- 【IOS 开发学习总结-OC-16】★★objective-c面向对象之——类的继承