POJ 1258 Agri-Net(Prim)

( ̄▽ ̄)"

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdlib>
#include<vector>
using namespace std;
typedef long long ll;
const int INF=10e7;
const int MAXN=110;
int cost[MAXN][MAXN];
int lowcost[MAXN];
bool vis[MAXN];

int Prim(int n)
{
    int ans=0;
    int minn,k;

    memset(vis,0,sizeof(vis));
    vis[1]=1;

    for(int i=1;i<=n;i++)
        lowcost[i]=cost[1][i];
    for(int i=1;i<=n-1;i++)
    {
        minn=INF;k=-1;
        for(int j=1;j<=n;j++)
            if(!vis[j]&&minn>lowcost[j])
            {
                minn=lowcost[j];
                k=j;
            }
        if(k==-1) break;
        ans+=minn;vis[k]=1;
        for(int j=1;j<=n;j++)
            if(!vis[j]&&lowcost[j]>cost[k][j])
                lowcost[j]=cost[k][j];
    }
    return ans;
}

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                scanf("%d",&cost[i][j]);
        printf("%d\n",Prim(n));
    }
    return 0;
}
时间: 2024-10-18 23:51:34

POJ 1258 Agri-Net(Prim)的相关文章

POJ 1251 Jungle Roads (prim)

D - Jungle Roads Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1251 Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extr

POJ 1789 -- Truck History(Prim)

 POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int maxn = 2000 + 10; 6 const int INF = 1000000; 7 int n;//有几个卡车 8 char str[maxn][10]; 9 int d[ma

POJ 2253-Frogger (Prim)

题目链接:Frogger 题意:两只青蛙,A和B,A想到B哪里去,但是A得弹跳有限制,所以不能直接到B,但是有其他的石头作为过渡点,可以通过他们到达B,问A到B的所有路径中,它弹跳最大的跨度的最小值 PS:最小生成树过的,刚开始用Dijstra做,Kao,精度损失的厉害,对于Dijksra的变形不大会变啊,看了Discuss有人用最小生成树过,我一划拉,还真是,敲了,就过了,等会研究研究最短路的各种变形,有模板不会变,不会灵活应用,渣渣就是渣渣. ME               Time 10

poj 1751 Highways (prim )

Highways Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9080   Accepted: 2536   Special Judge Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian

poj 2349 Arctic Network (prim算法)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10525   Accepted: 3470 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

POJ 3253 Fence Repair (优先队列)

POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN (1 ≤ N ≤ 20,000) planks of wood, each having some integer lengthLi (1 ≤ Li ≤ 50,000) units. He the

POJ 3292 Semi-prime H-numbers(数)

Semi-prime H-numbers Description This problem is based on an exercise of David Hilbert, who pedagogically suggested that one study the theory of 4n+1 numbers. Here, we do only a bit of that. An H-number is a positive number which is one more than a m

POJ 3301 Texas Trip (三分)

题目链接 题意 : 给你若干个点,让你找最小的正方形覆盖这所有的点.输出面积. 思路 : 三分枚举正方形两对边的距离,然后求出最大,本题用的是旋转正方形,也可以用旋转点,即点的相对位置不变. 正方形从0度到180度变化的过程中,把所有点覆盖,面积肯定是有一个最小峰值,是一个凸函数.因此可以用三分法解决.这里有一个难点就是已知两个定点的x,y坐标,过这两个点做两条平行线,平行线并与x轴成d度的角,怎么算出两条平行线的距离. d1 = fabs(cos(d)*(yi-yj)-sin(d)*(xi-x

POJ 1699 Best Sequence(DFS)

題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做的 1 //1699 2 #include <iostream> 3 #include <stdio.h> 4 #include <string.h> 5 #include <string> 6 7 using namespace std; 8 9 string

POJ 1160 Post Office (动态规划)

Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15412   Accepted: 8351 Description There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each villa