zoukankan      html  css  js  c++  java
  • 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.
    
    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. 
    最短路径的裸题,没啥难的,用spfa直接过。唯一注意的是可能会有平行边,需要排除。

    代码:

    #include<stdio.h>
    #include<cstring>
    #include<iostream>
    #include<queue>
    #include<cstring>
    #include<vector>
    #include<set>
    #include<algorithm>
    #include<cmath>
    
    #define INF 0x3f3f3f3f
    #define MIN(a,b) a>b?b:a
    
    using namespace std;
    
    int board[1005][1005];
    int len[1005];
    bool book[1005];
    int T,N;
    
    void Spfa()
    {
    	queue<int>Q;
    	Q.push(1);
    	book[1] = true;
    	while(!Q.empty())
    	{
    		int mid = Q.front();
    		Q.pop();
    		book[mid] = false;
    		for(int i=1 ; i<=N ; i++)
    		{
    			if(len[mid]+board[mid][i]<len[i])
    			{
    				len[i] = len[mid]+board[mid][i];
    				if(book[i] == false)
    				{
    					Q.push(i);
    					book[i] = true;
    				}
    			}
    		}
    	}
    }
    
    int main()
    {
    	for(int i=2 ; i<1005 ; i++)
    	{
    		len[i] = INF;
    	}
    	memset(board,INF,sizeof(board));
    	scanf("%d %d",&T,&N);
    	while(T--)
    	{
    		int a,b,c;
    		scanf("%d %d %d",&a,&b,&c);
    		board[a][b] = board[b][a] = MIN(c,board[a][b]);
    	}
    	Spfa();
    	printf("%d",len[N]);
    	return 0;
    }


  • 相关阅读:
    Android启动报错:install failed invalid apk
    一个资源丰富的在线小程序社区推荐
    一个好用的在线微信二维码设计网站
    一个资源丰富的在线小程序社区推荐
    百度地图API的使用示例
    百度地图API的使用示例
    vue.js提交按钮时简单的if判断表达式示例
    vue.js代码开发最常见的功能集合
    如何运行vue项目(维护他人的项目)
    ES6-----学习系列七(函数扩展)
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514307.html
Copyright © 2011-2022 走看看