A - Til the Cows Come Home

题目链接:https://cn.vjudge.net/contest/289568#problem/A
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.
Farmer John’s field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1…N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Input

  • Line 1: Two integers: T and N
  • Lines 2…T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1…100.
    Output
  • Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
    Sample Input
    5 5
    1 2 20
    2 3 30
    3 4 20
    4 5 20
    1 5 100
    Sample Output
    90
    Hint
    INPUT DETAILS:
    There are five landmarks.
    OUTPUT DETAILS:
    Bessie can get home by following trails 4, 3, 2, and 1.

解题思路:最短路
dijkstr 算法
1.然后再从dis[]从最短路径未确定的路径找出的最小值,令其为确定的最短路径(假设这个顶点是u),令book[u]=1;
2.从u点松弛
3.重复以上1. 2点直至所有点的最短路径确定。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<iostream>
#include<cstdlib>
#define N 1010
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define WC 1e-6
typedef long long LL;
using namespace std;
int book[N],dis[N],map[N][N];
void dijkstr(int n)
{
    int t=n,minn,u,to;
    memset(book,0,sizeof(book));
    for(int i=1;i<=n;i++)
        dis[i]=INF;
    dis[1]=0;
    while(t--)
    {
        minn=INF;
        for(int i=1;i<=n;i++)
        {
            if(!book[i]&&dis[i]<minn)
            {
                u=i;
                minn=dis[i];
            }
        }
        book[u]=1;
        for(int i=1;i<=n;i++)//松弛
        {
            if(!book[i])
                {
                    to=dis[u]+map[u][i];
                    if(to<dis[i])
                        dis[i]=to;
                }
        }
    }
}
int main()
{
    int T,n;
  while(~scanf("%d%d", &T, &n))
    {
        memset(map, 0, sizeof(map));
        for(int i = 1; i <= n; i++)//初始化
            for(int j = 1; j <= n; j++)
                map[i][j] = INF;
        for(int i = 0; i < T; i++)//开始
        {
            int x, y, c;
            scanf("%d%d%d", &x, &y, &c);
            if(map[x][y] > c)
                map[x][y] = map[y][x] = c;
        }
        dijkstr(n);
        printf("%d\n", dis[n]);
    }
    return 0;
}
全部评论

相关推荐

代码飞升AL:同学院本建议你换一个项目 就算你不去特意搜也应该知道点评不能写吧 保持投递不要停 然后快速弄一个项目换上去 公司就别挑了 我第一段120一天 快速跳就行
点赞 评论 收藏
分享
合适才能收到offe...:些许风霜罢了查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务