poj 2387 Til the Cows Come Home(最短路)

题目链接:http://poj.org/problem?id=2387

题目大意:求点1到点n的最短距离

1、Dijkstras算法

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

#define maxn 1010
#define INF 0x3f3f3f
int map[maxn][maxn];
int vis[maxn];  //标记最短路的点
int dis[maxn];  //源点到其他的点最短的距离

void setmap(int m)
{
    memset(map,INF,sizeof(map));
    int a,b,c;
    for(int i=0;i<m;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        if(map[a][b]>c)           //双向边处理
            map[a][b]=map[b][a]=c;
    }
}

void dijkstra(int n,int s)
{
    memset(dis,INF,sizeof(dis));
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
        dis[i]=map[s][i];
    for(int i=1;i<=n;i++)//n次松弛
    {
        int pos;
        int valu=INF;
        for(int j=1;j<=n;j++)//找到某一点的最短路径
            if(!vis[j] && dis[j]<valu)
            {
                pos=j;
                valu=dis[j];
            }
        vis[pos]=1;//标记找到最短路径的点
        for(int j=1;j<=n;j++)//更新最短路径
        {
            if(!vis[j] && dis[j]>dis[pos]+map[pos][j])
                dis[j]=dis[pos]+map[pos][j];
        }
    }
}

int main()
{
    int n,m;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        setmap(m);
        dijkstra(n,1);
        printf("%d\n",dis[n]);
    }
    return 0;
}

2、Bellman_ford算法

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

struct point//x->y的花费cou
{
    int x,y,cou;
};

#define maxn 1010
#define INF 0x3f3f3f
point e[maxn*4];//记边
int dis[maxn];//源点到其它点的最短距离

void setmap(int m)
{
    for(int i=0;i<m;i++)
    {
        cin>>e[i].x>>e[i].y>>e[i].cou;
        e[i+m].y=e[i].x;              //双向处理
        e[i+m].x=e[i].y;              //
        e[i+m].cou=e[i].cou;          //
    }
}

void bellman_ford(int n,int m,int s)
{
    memset(dis,INF,sizeof(dis));
    dis[s]=0;
    for(int i=1;i<n;i++)
        for(int j=0;j<2*m;j++)
            if(dis[e[j].y]>dis[e[j].x]+e[j].cou)
                dis[e[j].y]=dis[e[j].x]+e[j].cou;
}

int main()
{
    int n,m;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        setmap(m);
        bellman_ford(n,m,1);
        cout<<dis[n]<<endl;
    }
}
时间: 2024-08-28 02:32:45

poj 2387 Til the Cows Come Home(最短路)的相关文章

poj 2387 Til the Cows Come Home -- 最短路dijstra

<span style="font-size:18px;">利用dijstra求单源最短路,利用floyd会超时的</span> #include <stdio.h> #include <string.h> #include <algorithm> #define inf 0x3f3f3f3f using namespace std; const int N = 1005; int map[N][N]; int vis[N];

POJ 2387 Til the Cows Come Home (最短路+Dijkstra)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29550   Accepted: 9935 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

POJ 2387 Til the Cows Come Home dijkstra算法 用邻接表和邻接矩阵

题目如下: Til the Cows Come Home Time Limit: 1000MS        Memory Limit: 65536K Total Submissions: 27726        Accepted: 9353 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wa

poj 2387 Til the Cows Come Home(dijkstra算法)

题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int map[2010][2010],Min,node[2010],vis[2010],t,q; 5 const int INF=9999999; 6 7 vo

[2016-04-02][POJ][2387][Til the Cows Come Home]

时间:2016-04-02 10:34:36 星期六 题目编号:[2016-04-02][POJ][2387][Til the Cows Come Home] 题目大意:给定n个节点和t条路,求n到1的最短路长度 分析:跑一次最短路即可 遇到的问题: 据说是多重边,如果是用邻接矩阵的就要更新最小值, 此题是先输入t,再输入n,输入的时候读错,无限WA- #include <queue> #include <cstring> #include <cstdio> using

POJ 2387 Til the Cows Come Home (Dijkstra)

题目链接:POJ 2387 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possib

POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Accepted: 15899 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for

POJ 2387 Til the Cows Come Home Dijkstra求最短路径

Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.

怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33015   Accepted: 11174 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

POJ 2387 Til the Cows Come Home

题目连接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get ba