1005

Problem Description

省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全省畅通需要的最低成本。<br>

Input

测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( 1< N < 100 );随后的 N(N-1)/2 行对应村庄间道路的成本及修建状态,每行给4个正整数,分别是两个村庄的编号(从1编号到N),此两村庄间道路的成本,以及修建状态:1表示已建,0表示未建。<br><br>当N为0时输入结束。

Output

每个测试用例的输出占一行,输出全省畅通需要的最低成本。

Sample Input

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

Sample Output

3 1 0

解题思路:

prim算法求最小生成树

代码:

#include<iostream>
using namespace std;
const int N=110;
const int INF=0x3f3f3f3f;
int n,ans;
int imap[N][N],dis[N],vis[N];
void Prim(){
     int i;
    for(i=1;i<=n;i++){
        dis[i]=imap[1][i];
        vis[i]=0;
    }
    dis[1]=0;
    vis[1]=1;
    int j,k,tmp;
    for(i=1;i<=n;i++){
        tmp=INF;
        for(j=1;j<=n;j++)
            if(!vis[j] && tmp>dis[j]){
                k=j;
                tmp=dis[j];
            }
        if(tmp==INF)
            break;
        vis[k]=1;
        ans+=dis[k];
        for(j=1;j<=n;j++)
            if(!vis[j] && dis[j]>imap[k][j])
                dis[j]=imap[k][j];
    }  

}  

int main(){
while(~scanf("%d",&n)&&n){
      int ii,jj,temp1,temp2;
      for(int i=0;i<n*(n-1)/2;i++)
      { scanf("%d %d",&ii,&jj);
        scanf("%d",&temp1);
           scanf("%d",&temp2);
           if(temp2)
        {imap[ii][jj]=0;
        imap[jj][ii]=0;}
        else
        {imap[ii][jj]=temp1;
         imap[jj][ii]=temp1;
        }
    }  

        ans=0;
        Prim();
        printf("%d\n",ans);
    }
    return 0;
}  
时间: 2024-08-05 19:27:43

1005的相关文章

CSUFT 1005 Coffin Tiles

1005: Coffin Tiles Time Limit: 1 Sec      Memory Limit: 128 MB Submit: 2      Solved: 2 Description The Pumpkin King has a great idea for this Christmas: Personalized coffins for all the good little boys and girls! To make them extra special, Jack ha

PAT 乙级 1005 继续(3n+1)猜想 (25) C++版

1005. 继续(3n+1)猜想 (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对n=3进行验证的时候,我们需要计算3.5.8.4.2.1,则当我们对n=5.8.4.2进行验证的时候,就可以直接判定卡拉兹猜想的真伪,而不需要重

杭电女生赛1001 1002 1003 1005 1008 hdu6023 6024 6025 6027 6030

代码先贴这里 #include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set" #include "map" #include "algorithm&quo

HDU - 1005 Number Sequence(简单矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 题意:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 就是这道题目,然而找了一晚上的错误 \("▔□▔)/\("▔□▔)/\("▔□▔)/. 1 #include <iostream> 2 #include <cstring> 3 using namespace std;

1005 生日礼物

1005 生日礼物 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 9月12日是小松的朋友小寒的生日.小松知道小寒特别喜欢蝴蝶,所以决定折蝴蝶作为给小寒的生日礼物.他来到了PK大学最大的一家地下超市,在超市里,小松找到了n种可以用来折纸的本子.每种类型的本子里有若干不同颜色的纸若干张,当然同种类型的本子一定是完全一样的,而不同种类型的本子不一定完全不一样.他统计了一下,这里总共有n种不同类型的可以用来折纸的本子,每种本子各有bi

PAT甲级1005 Spell It Right

题目:PAT甲级 1005 题解:水题.看到题目的第一时间就在想一位一位的mod,最后一加一转换就完事了.结果看到了N最大为10的100的次方,吓得我赶紧放弃这个想法... 发现碰到这种情况用字符串十分好用,这道题应该考察的就是这一点.大致思路就是把数字的每一位放到字符串中,然后通过ASCII码得到每一位的相加结果num,然后把num一位一位的放到stack中,使用stack是因为它先进先出的特性,最后输出就行了. 代码: 1 #include<cstdio> 2 #include<qu

hdu 1005

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 思路:找规律题 1 #include<stdio.h> 2 main() 3 { 4 int s[100]; 5 int a,b,n,i; 6 while(scanf("%d%d%d",&a,&b,&n)!=EOF&&(a||b||n)) 7 { 8 s[1]=1; 9 s[2]=1; 10 for(i=3;i<=49;i++

ural 1005 Stone Pile

这是道01背包题   ,虽然背包不会  ,但是还是看出来了,递推公式写啊写没写出来,后来一同学说是dfs,这我就开始了A了, 题意是给你n个重量是Wn的石头  让你放到两个包里面,看他们两个差值最小,并且输出这个差值. dfs代码 #include <stdio.h> int sum; int h,T; int a[100]; void dfs (int x,int y) { if(x==T) { if(y>h) h=y ; return ; } if(h==sum/2) return

poj 1005:I Think I Need a Houseboat(水题,模拟)

I Think I Need a Houseboat Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 85149   Accepted: 36857 Description Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land,

1005 琐碎的区间

1005: 琐碎的区间 时间限制: 4 Sec  内存限制: 256 MB提交: 100  解决: 10[提交][状态][讨论版] 题目描述 给出一个长度为 n 的整数序列 A[1..n],有三种操作: 1 l r x :  把[l, r]区间的每个数都加上 x 2 l r :  把[l, r]  区间每个 A[i]变为sqrt(a[i])的整数部分3 l r :  求[l, r]  区间所有数的和 其中 l 和 r 和 x 都代表一个整数 输入 第一行一个 T,表示数据组数. 对于每组数据 L