Prim_poj2075 Tangled in Cables

原创 2012年03月26日 21:33:26

题意:最小生成树问题,加上简单的字符串处理

/*
Sample Input

100.0
4
Jones
Smiths
Howards
Wangs
5
Jones Smiths 2.0
Jones Howards 4.2
Jones Wangs 6.7
Howards Wangs 4.0
Smiths Wangs 10.0
Sample Output

Need 10.2 miles of cable

*/
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
#include<iostream>

#define MAXN 250
#define INF 1000000000

using namespace std;

map<string, int>  bianhao;

double Gmap[MAXN][MAXN];
double LowValue[MAXN];
bool visited[MAXN];

double Prim(int n){
    memset(visited, 0, sizeof(visited));
    visited[0] = 1;
    for(int i = 0; i < n; i++){
        LowValue[i] = Gmap[0][i];
    }
    int st;
    double sum = 0;
    for(int i = 1; i < n; i++){
        double minV = INF;
        for(int j = 0; j < n; j++){
            if(!visited[j] && LowValue[j] < minV){
                minV = LowValue[j];
                st = j;
            }
        }
        visited[st] = 1;sum += minV;
        for(int j = 0; j < n; j++){
            if(!visited[j] && Gmap[st][j] < LowValue[j]) LowValue[j] = Gmap[st][j];
        }
    }
    return sum;
}
void inint(int n){
    for(int i = 0; i< n + 10;i++){
        for(int j = 0; j < n +10; j++){
            Gmap[i][j] = INF;
        }
    }
}
int main(){
    double calen;
    int n, m;
    string str,str1, str2;
    double l;
    while(cin>>calen){
        cin>>n;
        inint(n);
        bianhao.clear();
        for(int i = 0; i < n; i++){
            cin>>str;
            bianhao[str] = i;
        }
        cin>>m;
        for(int i = 0; i < m; i++){
            cin>>str1>>str2>>l;
            Gmap[bianhao[str1]][bianhao[str2]] = Gmap[bianhao[str2]][bianhao[str1]] = l;
        }
        double len = Prim(n);
        if(calen - len >= 0.0000000001)  printf("Need %.1f miles of cable\n", len);
        else printf("Not enough cable\n");
    }
}


POJ 2075 Tangled in Cables 最小生成树 Kruskal && Prim

最小生成树 , Kruscal && Prim ,将房屋转变为编号,就是一 模板题 You are the owner of SmallCableCo and have purchased...

poj2075Tangled in Cables (最小生成树之prim)

Problem Description You are the owner of SmallCableCo and have purchased the franchise rights for a...

poj_2075 Tangled in Cables

Tangled in Cables Time Limit: 1000MS Memory Limit: 30000K Total Submissions...
  • CSDN515
  • CSDN515
  • 2012年08月16日 18:03
  • 736

POJ 2075 Tangled in Cables (kruskal算法 MST + map)

POJ 2075 Tangled in Cables (kruskal算法 MST + map)

POJ 2075 Tangled in Cables

链接:http://poj.org/problem?id=2075 题意:给N个城市,用电缆将所有城市连接起来。 思路:最小生成树模板题,kruskal算法的应用。 代码: #include ...

POJ 2075 Tangled in Cables (c++/java)

给你一些人名,然后给你n条连接这些人名所拥有的房子的路,求用最小的代价求连接这些房子的花费是否满足要求。 附带C++ /JAVA实现...

POJ 2075 Tangled in Cables(最小生成树)

POJ 2075 Tangled in Cables(最小生成树) http://poj.org/problem?id=2075 题意: 现在一个小镇要通过电缆来通电.给你小镇的房屋数目N和对应的名称...

poj 2075 Tangled in Cables

Tangled in Cables Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other)...

POJ 2075 Tangled in Cables(Kruskal算法)

Tangled in Cables Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4014 ...

poj2075 Tangled in Cables

题目: Description You are the owner of SmallCableCo and have purchased the franchise rights for a...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Prim_poj2075 Tangled in Cables
举报原因:
原因补充:

(最多只允许输入30个字)