Til the Cows Come Home
来源:互联网 发布:车辆mod修改软件 编辑:程序博客网 时间:2024/06/05 11:00
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.
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.
* 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.
* 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.
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
5 51 2 202 3 303 4 204 5 201 5 100
90
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
#include <iostream>#include <cstdio>#include <cstring>#include <queue> #include <algorithm>using namespace std;#define INF 0x3f3f3f3fint disance[2010]; //记录到起点的距离 vector<int > edge[2010]; // 存相连的边 int lengh[2010][2010]; //存相连边的长度 int n,t;struct Pair // 定义结构体 { int first; //存点 int second; // 存距离 bool friend operator < (Pair a,Pair b) { return a.second > b.second; }}pt ,st;void dijikstra(){ memset ( disance , INF , sizeof(disance)); // 将距离初始化 bool vis[2200]; // 定义 标记数组 memset (vis , false , sizeof(vis)); disance[1] = 0; //s为起点 赋0 pt.first = 1 ; pt.second = 0; //起点距离为0 priority_queue<Pair> Q; // 定义优先队列 Q.push(pt); while(! Q.empty()) { pt = Q.top() ; //printf ("==%d %d==\n",pt.first,pt.second); Q.pop() ; if( vis[pt.first] ) continue; vis[pt.first] = true; for(int i = 0 ; i < edge[pt.first].size(); i++) { st.first = edge[pt.first][i]; st.second = pt.second + lengh[pt.first][st.first]; if( st.second < disance[st.first]) { disance[st.first] = st.second; Q.push(st); }} }}int main(){ scanf("%d %d",&t,&n); int a,b,x; memset(lengh , -1 , sizeof(lengh)); for(int i = 1; i <= t ; i++) { scanf("%d %d %d",&a,&b,&x); edge[a].push_back(b); edge[b].push_back(a); // 双向建边if( lengh[a][b] == -1 ) //说明边还未用 { lengh[a][b] = lengh[b][a] = x; } else { lengh[a][b] = lengh[b][a] = min( x, lengh[a][b] ); //重边时取最小的那个 } } dijikstra(); if(disance[n] == INF) printf("-1\n"); else printf("%d\n",disance[n]);return 0;}
阅读全文
1 0
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- PKU2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- ubuntu遇到的问题之“Package 'vim' has no installation candidate”
- hadoop-2.7.3分布式部署
- 百度地图的简单开发之方向传感器实现定位功能
- 1459 迷宫游戏(51NOD)
- python的入门教程 给大家推荐一下
- Til the Cows Come Home
- ILRuntime第四课Inheritance
- HttpClient4.5.3添加Cookie和Header的User-Agent
- ubuntu安装libtbb.so.2
- 与非 乱搞233
- mysql数据库锁定机制
- mysql 4种插入语句的区别。
- 类型重定义、基类未定义
- There was an error when generating this application with JHipster解决