最短路入门题

http://acm.hdu.edu.cn/showproblem.php?pid=2544

DJ

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define N 1000001
using namespace std;
int map[101][101];
int n,m;
int v[101],dis[101];
void D()
{
    memset(v,0,sizeof(v));
    for(int i=1;i<=n;i++)
        dis[i]=map[1][i];
    dis[1]=0;
    v[1]=1;
    int min,k;
    for(int i=1;i<n;i++)
    {
        min=N;
        for(int j=1;j<=n;j++)
        {
            if(!v[j]&&min>dis[j])
            {
                min=dis[j];
                k=j;
            }
        }
        v[k]=1;
        for(int j=1;j<=n;j++)
        {
            if(!v[j]&&map[k][j]+dis[k]<dis[j])
            {
                dis[j]=map[k][j]+dis[k];
            }
        }
    }
    printf("%d\n",dis[n]);
}
int main()
{
    int x,y,z;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0) break;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                map[i][j]=N;
                map[j][i]=N;
            }
            map[i][i]=0;
        }
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&z);
            if(map[x][y]>z)
            {
                map[x][y]=z;
                map[y][x]=z;
            }
        }
        D();
    }
    return 0;
}
BE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define N 1000001
int n,m,flag,t;
struct node
{
    int x,y,w;
}edge[20002];
int dis[101];
void add(int x,int y,int w)
{
    edge[t].x=x;
    edge[t].y=y;
    edge[t++].w=w;
}
void B()
{
    for(int i=1;i<=n;i++)
        dis[i]=N;
    dis[1]=0;
    for(int i=1;i<n;i++)
    {
        flag=0;
        for(int j=0;j<t;j++)
        {
            if(edge[j].w+dis[edge[j].x]<dis[edge[j].y])
            {
                   dis[edge[j].y]=dis[edge[j].x]+edge[j].w;
                   flag=1;
            }

        }
        if(flag==0) break;
    }
    printf("%d\n",dis[n]);
}
int main()
{
    int x,y,z;
    while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
    {
        t=0;
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);
            add(y,x,z);
        }
        B();
    }
    return 0;
}
F
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define N 1000001
using namespace std;
int map[101][101];
int n,m;
void F()
{
    for(int k=1;k<=n;k++)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(map[i][k]+map[k][j]<map[i][j])
                    map[i][j]=map[i][k]+map[k][j];
            }
        }
    }
}
int main()
{
    int x,y,z;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0) break;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                map[i][j]=N;
                map[j][i]=N;
            }
            map[i][i]=0;
        }
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&z);
            if(map[x][y]>z)
            {
                map[x][y]=z;
                map[y][x]=z;
            }
        }
        F();
        printf("%d\n",map[1][n]);
    }
    return 0;
}
 SPFA
#include <stdio.h>
#include <string.h>
#include <string.h>
#define N 1000001
int n,m;
struct node
{
    int x,y,z,next;
}edge[20004];
int head[102];
int dis[102];
int v[102];
int t;
void init()
{
    memset(head,-1,sizeof(head));
    t=0;
}
void add(int x,int y,int z)
{
    edge[t].x=x;
    edge[t].y=y;
    edge[t].z=z;
    edge[t].next=head[x];
    head[x]=t++;
}
int q[20004];
void SPFA()
{
    memset(v,0,sizeof(v));
    for(int i=1;i<=n;i++)
        dis[i]=N;
    dis[1]=0;
    int e=0;
    int s=0;
    int tt;
    q[e++]=1;
    v[1]=1;
    while(s<e)
    {
        tt=q[s++];
        v[tt]=0;//细心
        for(int i=head[tt];i!=-1;i=edge[i].next)
        {
            if(dis[tt]+edge[i].z<dis[edge[i].y])
            {
                dis[edge[i].y]=dis[tt]+edge[i].z;
                if(v[edge[i].y]==0)
                {
                   // if(dis[edge[i].y]>dis[q[s]])
                    q[e++]=edge[i].y;
                  //  else q[--s]=edge[i].y;
                    v[edge[i].y]=1;
                }
            }
        }
    }
    printf("%d\n",dis[n]);

}
int main()
{
    int x,y,z;
    while(scanf("%d%d",&n,&m)!=EOF&&(m||n))
    {
        init();
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);
            add(y,x,z);
        }
        SPFA();
    }
}
 

最短路入门题,布布扣,bubuko.com

时间: 2024-12-16 22:59:57

最短路入门题的相关文章

POJ1502(最短路入门题)

MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7471   Accepted: 4550 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchic

hdu 2767 Proving Equivalences(强连通入门题)

1 /************************************************* 2 Proving Equivalences(hdu 2767) 3 强连通入门题 4 给个有向图,求至少加多少条边使得图是所有点都是强连通的 5 由a->b->c->a易知n个点至少要n条边,每个出度和入度都要大 6 于1.先求所有所有强连通分量,把每个强连通分量看成一个点 7 在找每个点的出度和入度,最后还差的出度和入度的最大值就是 8 答案. 9 10 ************

hdu 5001 walk 概率dp入门题

Description I used to think I could be anything, but now I know that I couldn't do anything. So I started traveling. The nation looks like a connected bidirectional graph, and I am randomly walking on it. It means when I am at node i, I will travel t

hdu1796:容斥入门题

简单的容斥入门题.. 容斥基本的公式早就知道了,但是一直不会写. 下午看到艾神在群里说的“会枚举二进制数就会容斥”,后来发现还真是这样.. 然后直接贴代码了 #include <iostream> #include <stdio.h> #include<string.h> #include<algorithm> #include<string> #include<ctype.h> using namespace std; long l

HDU 5521.Meeting 最短路模板题

Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3361    Accepted Submission(s): 1073 Problem Description Bessie and her friend Elsie decide to have a meeting. However, after Farmer Jo

hdu1695 GCD(莫比乌斯入门题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意: 给出n.m.k ,求出1<=x<=n, 1<=y<=m 且gcd(x,y) == k 的(x,y)的对数 解析: 显然就是求 [1,n/k] 与 [1, m/k]有多少数对的最大公约数是1 莫比乌斯入门题 我们设 为满足且和的的对数 为满足且和的的对数 那么,很显然,反演后得到 我们所需要的答案便是  f(1) = ∑i=1μ(i)*(n/i)*(m/i)  ,求解这个式

网络流最经典的入门题 各种网络算法都能AC。

Drainage Ditches 题目抽象:给你m条边u,v,c.   n个定点,源点1,汇点n.求最大流.  最好的入门题,各种算法都可以拿来练习 (1):  一般增广路算法  ford() 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string&g

poj1511/zoj2008 Invitation Cards(最短路模板题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds      Memory Limit: 65536 KB In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fa

hdu 1754:I Hate It(线段树,入门题,RMQ问题)

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 33726    Accepted Submission(s): 13266 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感.不管你喜不喜欢,现在需要你做的是,就是按照老师的要求