POJ--2387--Til the Cows Come Home (最短路 弗洛伊德)

 
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

#define INF 0xfffffff
#define N 1002

int n, m, G[N][N], vis[N], dist[N];

void IN()
{
    memset(vis, 0, sizeof(vis));

    for(int i=1; i<=n; i++)
    {
        dist[i]=INF;
        for(int j=1; j<=i; j++)
        {
            G[i][j]=G[j][i]=INF;
        }
    }
}

void  DIST(int S, int E)
{
    dist[S]=0;

    for(int i=1; i<=n; i++)
    {
        int index=1, MIN=INF;
        for(int j=1; j<=n; j++)
        {
            if(vis[j]==0 && dist[j]<MIN)
                MIN=dist[j], index=j;
        }

        vis[index]=1;
        for(int j=1; j<=n; j++)
        {
            if(vis[j]==0 && G[index][j]+dist[index]<dist[j])
            {
                dist[j]=G[index][j]+dist[index];
            }
        }
    }

    cout << dist[E] << endl;
}

int main()
{
    while(scanf("%d%d", &m, &n)!=EOF)
    {
        int i, a, b, c;

        IN();

        for(i=0; i<m; i++)
        {
            scanf("%d%d%d", &a, &b, &c);

            G[a][b]=G[b][a]=min(G[a][b], c);
        }

        DIST(1, n);
    }
    return 0;
}

  

时间: 2024-10-14 03:35:54

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