码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1863 [【最小生成树】+hdu2544【floyed】+hdu1874【dijdtra】~~~模板复习~~~

时间:2017-09-21 19:23:09      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:class   std   int   get   nbsp   mem   color   ret   include   

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1863

#include<stdio.h>
#include<string.h>
#define inf 0x3f3f3f3f
#define N 120
int book[N],w[N][N],min,dis[N];

int main()
{
    int m,n,i,j,t1,t2,t3,s,e,u,flag,sum;
    while(scanf("%d%d",&m,&n),m!=0)
    {
        for(i = 0; i <= n; i ++)
            for(j = 0; j <= n; j ++)
                if(i == j)
                    w[i][j] = 0;
                else
                    w[i][j] = inf;
        for(i = 1; i <= m; i ++)
        {
            scanf("%d%d%d",&t1,&t2,&t3);
            if(t3 < w[t1][t2])
            {
                w[t1][t2] = t3;
                w[t2][t1] = t3;
            }
        }
        if(m < n-1)
        {
            printf("?\n");
            continue;
        }
        memset(book,0,sizeof(book));    
        for(i = 1; i <= n; i ++)
            dis[i] = w[1][i];
        book[1] = 1;
        sum = 0;
        for(i = 1; i < n; i ++)
        {
            min = inf;
            for(j = 1; j <= n; j ++)
            {
                if(!book[j]&&dis[j] < min)
                {
                    u = j;
                    min = dis[j];
                }
            }
            book[u] = 1;
            sum += min;
            for(j = 1; j <= n; j ++)
                if(!book[j]&&dis[j] > w[u][j])
                    dis[j] =  w[u][j];
        }
        flag = 0;
        for(i = 1; i <= n; i ++)
            if(!book[i])
            {
                flag = 1;
                break;
            }
        if(flag)
            printf("?\n");
        else
            printf("%d\n",sum);
    }
    return 0;
}

 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874

 

#include<stdio.h>
#include<string.h>
#define inf 0x3f3f3f3f
#define N 110
int w[N][N];

int main()
{
    int n,m,i,j,k,t1,t2,t3;
    while(scanf("%d%d",&n,&m),n!=0&&m!=0)
    {
        for(i = 0; i <= n; i ++)
            for(j = 0; j <= n; j ++)
                if(i == j)
                    w[i][j] = 0;
                else
                    w[i][j] = inf;
        for(i = 1; i <= m; i ++)
        {
            scanf("%d%d%d",&t1,&t2,&t3);
            if(t3 < w[t1][t2])
            {
                w[t1][t2] = t3;
                w[t2][t1] = t3;
            }
            
        }
        for(k = 1; k <= n; k ++)
            for(i = 1; i <= n; i ++)
                for(j = 1; j <= n; j ++)
                    if(w[i][j] > w[i][k]+w[k][j])
                        w[i][j] = w[i][k]+w[k][j];
        printf("%d\n",w[1][n]);
    }
    return 0;
}

 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544

#include<stdio.h>
#include<string.h>
#define inf 0x3f3f3f3f
#define N 220
int book[N],w[N][N],min,dis[N];

int main()
{
    int m,n,i,j,t1,t2,t3,s,e,u;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i = 0; i <= n; i ++)
            for(j = 0; j <= n; j ++)
                if(i == j)
                    w[i][j] = 0;
                else
                    w[i][j] = inf;
        for(i = 1; i <= m; i ++)
        {
            scanf("%d%d%d",&t1,&t2,&t3);
            if(t3 < w[t1][t2])
            {
                w[t1][t2] = t3;
                w[t2][t1] = t3;
            }
        }
        scanf("%d%d",&s,&e);
        memset(book,0,sizeof(book));    
        for(i = 0; i < n; i ++)
            dis[i] = w[s][i];
        book[s] = 1;
        for(i = 1; i < n; i ++)
        {
            min = inf;
            for(j = 0; j < n; j ++)
            {
                if(!book[j]&&dis[j] < min)
                {
                    u = j;
                    min = dis[j];
                }
            }
            book[u] = 1;
            for(j = 0; j < n; j ++)
                if(!book[j]&&dis[j] > dis[u]+w[u][j])
                    dis[j] = dis[u] + w[u][j];
        }
        if(dis[e] == inf)
            printf("-1\n");
        else
            printf("%d\n",dis[e]);
    }
    return 0;
}

 

hdu 1863 [【最小生成树】+hdu2544【floyed】+hdu1874【dijdtra】~~~模板复习~~~

标签:class   std   int   get   nbsp   mem   color   ret   include   

原文地址:http://www.cnblogs.com/chengdongni/p/7570049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!