最短路A - Til the Cows Come Home

题意:求从1到n的最短路距离

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>

using namespace std;

#define maxn 2005

int head[maxn];
int cnt;
int visit[maxn];
int dist[maxn];
const int oo=0x3f3f3f3f;
struct node
{
    int u,v,next,w;
}e[maxn];

void Init(int Start,int End)
{
    for(int i=Start;i<=End;i++)
        dist[i]=oo;
    memset(head,-1,sizeof(head));
    for(int i=Start;i<=End;i++)
        e[i].next=-1;
}

void add(int u,int v,int w)
{
    e[cnt].u=u;
    e[cnt].v=v;
    e[cnt].w=w;
    e[cnt].next=head[u];
    head[u]=cnt++;
}

int Spfa(int Start,int End)
{
    stack<int>sta;
    dist[Start]=0;
    sta.push(Start);
    memset(visit,0,sizeof(visit));
    while(sta.size())
    {
        int s=sta.top();
        sta.pop();
        visit[s]=0;
        for(int i=head[s];i!=-1;i=e[i].next)
        {
            int u=e[i].u;
            int v=e[i].v;
            int w=e[i].w;
            if(dist[u]+w<dist[v])
            {
                dist[v]=dist[u]+w;
                if(visit[v]==0)
                {
                    visit[v]=1;
                    sta.push(v);
                }
            }
        }
    }
    return dist[End];
}

int main()
{
    int t,n;
    while(~scanf("%d",&t))
    {
        scanf("%d",&n);
        Init(1,n);
        int u,v,w;
        for(int i=1;i<=t;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);//由于未添加这个边,错啦n多次。
        }
        int ans=Spfa(1,n);
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-03 21:54:12

最短路A - Til the Cows Come Home的相关文章

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

POj2387——Til the Cows Come Home——————【最短路】

A - Til the Cows Come Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice 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 F

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 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

[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

kuangbin专题专题四 Til the Cows Come Home POJ - 2387

题目链接:https://vjudge.net/problem/POJ-2387 题意:从编号为n的城市到编号为1的城市的最短路. 思路:dijkstra模板题,直接套板子,代码中我会带点注释给初学者看. 1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <cstdio> 5 #include <string> 6 using namespac

POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37662   Accepted: 12836 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算法)

题目链接: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

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.