hoj 1632
2012-02-13 19:01
267 查看
#include <iostream> using namespace std; const int N=30; const int Max=105;//cost int cost ,d ,total,n; bool f ; void init() { for(int i=1;i<=n;i++) { f[i]=0; d[i]=Max; for(int j=i+1;j<=n;j++) { cost[i][j]=Max; cost[j][i]=Max; } } return; } void path() { int k=1; f[1]=1; d[1]=0; total=0; for(int j=2;j<=n;j++) { int mcost=Max; for(int i=2;i<=n;i++) { if(!f[i]) { if(d[i]<mcost) { mcost=d[i]; k=i; } } }//for f[k]=1; total+=d[k]; for(int t=2;t<=n;t++) { if((!f[t])&&(d[t]>cost[k][t])) d[t]=cost[k][t]; } } return; } int main() { while(cin>>n) { if(n==0) break; init(); for(int i=1;i<n;i++) { int num,weight; char first,second; cin>>first>>num; for(int j=0;j<num;j++) { cin>>second>>weight; cost[first-'A'+1][second-'A'+1]=weight; cost[second-'A'+1][first-'A'+1]=weight; } }//for for(int k=2;k<=n;k++) d[k]=cost[1][k]; path(); cout<<total<<endl; } return 0; }
相关文章推荐
- Hoj 1632 Jungle Roads
- [HOJ]1632、[POJ]1251 Jungle Roads、c++/Java解决
- [hoj 1632]Jungle Roads[Kruskal最小生成树模板题]
- HOJ过200了
- 1632 Sumsets【转】
- Maze Stretching--HOJ 11879
- HOJ 11958 Hyper 解题报告
- hoj 1863 最小生成树
- hoj 2608 Assemble
- HOJ 3087 Discover's Problem I
- hoj 3008 Matryoshka Dolls Again 最大独立子集
- hoj1952 power string
- hoj 2578 Super_Stack 模拟栈
- Radar Installation 贪心 hoj
- HOJ 1898 Babelfish(Trie树)
- hoj 1035 factorial
- HOJ 1113 Stockbroker Grapevine (Floyd最短路)
- HOJ 1599-------Box of Bricks
- HOJ 2597 Savings Account
- HOJ 2076 Series Determination