POJ 2075

#include<iostream>
#include<stdio.h>
#include<string>
#include<map>
#include<iomanip>
#define MAXN 500
#define inf 1000000000
typedef double elem_t;
using namespace std;

double _m[MAXN][MAXN];
int pre[MAXN];
map<string,int> coll;
map<string,int>::iterator pos1,pos2;

elem_t prim(int n,elem_t mat[][MAXN],int* pre){
    elem_t min[MAXN],ret=0;
    int v[MAXN],i,j,k;
    for (i=0;i<n;i++)
        min[i]=inf,v[i]=0,pre[i]=-1;
    for (min[j=0]=0;j<n;j++){
        for (k=-1,i=0;i<n;i++)
            if (!v[i]&&(k==-1||min[i]<min[k]))
                k=i;
        for (v[k]=1,ret+=min[k],i=0;i<n;i++)
            if (!v[i]&&mat[k][i]<min[i])
                min[i]=mat[pre[i]=k][i];
    }
    return ret;
}

int main()
{
    //freopen("acm.acm","r",stdin);
    double st;
    int i;
    int j;
    double ans;
    int num_name;
    int edge;
    string s;
    string s1;
    double len;
    for(i = 0; i < MAXN ; ++ i)
    {
        for(j = 0; j < MAXN; ++ j)
            _m[i][j] = inf;
    }
    cin>>st;
    cin>>num_name;
    for(i = 0; i < num_name; ++ i)
    {
        cin>>s;
        coll.insert(pair<string,int>(s,i));
    }
    cin>>edge;
    for(i = 0; i < edge; ++ i)
    {
        cin>>s;
        cin>>s1;
        cin>>len;
        pos1 = coll.find(s);
        pos2 = coll.find(s1);
        _m[pos1->second][pos2->second] = len;
        _m[pos2->second][pos1->second] = len;
    }
    if((ans = prim(num_name,_m,pre)) > st)
        cout<<"Not enough cable "<<endl;
    else
        cout<<"Need "<<setiosflags(ios::fixed)<<setprecision(1)<<ans<<" miles of cable"<<endl;
}
时间: 2024-10-26 02:31:00

POJ 2075的相关文章

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

http://poj.org/problem?id=2075 题目大意: 给你一些人名,然后给你n条连接这些人名所拥有的房子的路,求用最小的代价求连接这些房子的花费是否满足要求. 思路: 昨天20分钟的题,输入不小心写错了- -|||||看世界杯半场休息随便看了下发现了....T T 用map进行下标的映射,然后求MST即可. c++ #include<cstdio> #include<string> #include<map> #include<algorith

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

Tangled in Cables Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6039   Accepted: 2386 Description You are the owner of SmallCableCo and have purchased the franchise rights for a small town. Unfortunately, you lack enough funds to start

poj 2075 Tangled in Cables

Tangled in Cables Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) Total Submission(s) : 19   Accepted Submission(s) : 6 Problem Description You are the owner of SmallCableCo and have purchased the franchise rights for

POJ 2075:Tangled in Cables 【Prim】

Tangled in Cables Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) Total Submission(s) : 2   Accepted Submission(s) : 1 Problem Description You are the owner of SmallCableCo and have purchased the franchise rights for

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京

转载:poj题目分类(侵删)

转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K–0.50K:中短代码:0.51K–1.00K:中等代码量:1.01K–2.00K:长代码:2.01K以上. 短:1147.1163.1922.2211.2215.2229.2232.2234.2242.2245.2262.2301.2309.2313.2334.2346.2348

图论常用算法之一 POJ图论题集【转载】

POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:http://poj.org/ 1062* 昂贵的聘礼 枚举等级限制+dijkstra 1087* A Plug for UNIX 2分匹配 1094 Sorting It All Out floyd 或 拓扑 1112* Team Them Up! 2分图染色+DP 1125 Stockbroker

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)