POJ 2387-Til the Cows Come Home(Dijkstra+堆優化)
Til the Cows Come Home
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 40039 | Accepted: 13620 |
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 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.
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.
* 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.
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
Source
題目意思:
有N個點,給出從a點到b點的距離,當然a和b是互相可以抵達的,問從1到n的最短距離。
兩種解題思路:
①Dijkstra;
②使用STL的priority_queue實現。
①
/*
* Copyright (c) 2016, 煙臺大學計算機與控制工程學院
* All rights reserved.
* 檔名稱:dijkstra.cpp
* 作 者:單昕昕
* 完成日期:2016年3月30日
* 版 本 號:v1.0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define MAXN 1010
#define INF 0xfffffff//0X代表16進位制,後面是數字,十進位制是4294967295
using namespace std;
int cost[MAXN][MAXN],dis[MAXN],n,t;
bool used[MAXN];//標識是否使用過
void dijkstra(int s)
{
memset(used,0,sizeof(used));
fill(dis,dis+n+1,INF);
fill(used,used+n+1,false);
dis[s]=0;
while(true)
{
int v=-1;
//從未使用過的頂點中選擇一個距離最小的頂點
for(int u=0; u<n; ++u)
if(!used[u]&&(v==-1||dis[u]<dis[v]))
v=u;
if(v==-1) break;
used[v]=true;
for(int u=0; u<=n; ++u)
dis[u]=min(dis[u],dis[v]+cost[v][u]);
}
}
int main()
{
int a,b,l;
while(scanf("%d%d",&t,&n)!=EOF)
{
for(int i=0; i<=n; ++i)
for(int j=0; j<=n; ++j)
cost[i][j]=INF;//手動初始化,不能fill了……
for(int i=0; i<t; i++)
{
scanf("%d%d%d",&a,&b,&l);
if(l<cost[a][b])
cost[a][b]=cost[b][a]=l;//無向圖
}
dijkstra(1);
printf("%d\n",dis[n]);
}
return 0;
}
/*
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
*/
②使用STL的priority_queue實現。
記憶體佔用少:
/*
* Copyright (c) 2016, 煙臺大學計算機與控制工程學院
* All rights reserved.
* 檔名稱:queue.cpp
* 作 者:單昕昕
* 完成日期:2016年6月1日
* 版 本 號:v2.0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define INF 0xfffffff//0X代表16進位制,後面是數字,十進位制是4294967295
#define MAXN 1010
using namespace std;
int dis[MAXN],n,t;
struct edge
{
int to;
int cost;
};
vector<edge> G[MAXN];//邊表
typedef pair<int,int> P;
//first是最短距離,second是頂點的編號
void dijkstra(int s)
{
//通過指定greater<P>引數,堆按照first從大到小的順序取初值
priority_queue<P,vector<P>,greater<P> > que;
fill(dis,dis+n+1,INF);//初始化範圍要到n
dis[s]=0;
que.push(P(0,s));
while(!que.empty())
{
P p=que.top();
que.pop();
int v=p.second;
if(dis[v]<p.first) continue;
for(int i=0; i<G[v].size(); ++i)
{
edge e=G[v][i];
if(dis[e.to]>dis[v]+e.cost)//Dijkstra
{
dis[e.to]=dis[v]+e.cost;
que.push(P(dis[e.to],e.to));
}
}
}
}
int main()
{
int a,b,l;
while(scanf("%d%d",&t,&n)!=EOF)
{
for(int i=0; i<t; i++)
{
scanf("%d%d%d",&a,&b,&l);//a->b的距離是l
G[a].push_back({b,l});//這裡的語法要注意
G[b].push_back({a,l});
}
dijkstra(1);//起點是1
printf("%d\n",dis[n]);
}
return 0;
}
/*
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
*/
③
/*
* Copyright (c) 2016, 煙臺大學計算機與控制工程學院
* All rights reserved.
* 檔名稱:dijkstra.cpp
* 作 者:單昕昕
* 完成日期:2016年3月30日
* 版 本 號:v1.0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define inf 0xfffffff//0X代表16進位制,後面是數字,十進位制是4294967295
using namespace std;
int p[1003][1003],flag[1003],dis[1003],mmin,n,t;
void dijkstra()
{
memset(flag,0,sizeof(flag));
for(int i=2; i<=n; i++)
{
dis[i]=p[1][i];
}
flag[1]=1;
int pre;
for(int i=1; i<=n; i++)
{
mmin=inf;
for(int j=1; j<=n; j++)
{
if(flag[j]==0&&dis[j]<mmin)
{
mmin=dis[j];
pre=j;
}
}
if(mmin==inf)
break;
flag[pre]=1;
for(int j=1; j<=n; j++) //找最短路
{
if(flag[j]==0&&dis[pre]+p[pre][j]<dis[j])
{
dis[j]=dis[pre]+p[pre][j];
}
}
}
}
int main()
{
int a,b,l;
while(scanf("%d%d",&t,&n)!=EOF)
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
p[i][j]=inf;
}
}
for(int i=0; i<t; i++)
{
scanf("%d%d%d",&a,&b,&l);
if(l<p[a][b])
{
p[a][b]=p[b][a]=l;//無向圖
}
}
dijkstra();
printf("%d\n",dis[n]);
}
return 0;
}
相關文章
- POJ2387-Til the Cows Come Home
- POJ2387 Til the Cows Come Home【最短路 Dijkstra演算法】演算法
- POJ1511 Invitation Cards【Dijkstra+堆優化+前向星】優化
- POJ 1556 The Doors(Dijkstra+計算幾何)
- POJ 2186 Popular Cows(強連通分量縮點,Tarjan演算法)演算法
- POJ-1363 Rails-堆疊入門AI
- poj 1180 dp的斜率優化優化
- POJ 2195 Going Home 最小費用最大流Go
- POJ 2195 Going Home (最小費用最大流)Go
- 利用Decorator和SourceMap優化JavaScript錯誤堆疊優化JavaScript
- Cows in a Skyscraper G
- POJ3744 Scout YYF I (概率DP + 矩陣優化)矩陣優化
- 堆--優先佇列佇列
- 堆與優先佇列佇列
- 堆和優先佇列佇列
- LeetCode-Bulls and CowsLeetCode
- POJ 3253 Fence Repair 優先佇列AI佇列
- 堆和堆的應用:堆排序和優先佇列排序佇列
- [POJ 3415] Common Substrings (字尾陣列+單調棧優化)陣列優化
- Leetcode 299 Bulls and CowsLeetCode
- 圖的深度優先遍歷[非堆疊、堆疊實現]
- All good things come to an endGo
- 詞根:ven, vent = to come 來
- POJ 1724 ROADS(優先佇列+spfa)佇列
- POJ2431 Expedition (優先佇列)佇列
- POJ 2051(最小堆/優先佇列)佇列
- 二叉堆優先佇列佇列
- 堆——神奇的優先佇列(上)佇列
- POJ 2195-Going Home(KM演算法/最小費用最大流演算法)Go演算法
- 最短路-樸素版Dijkstra演算法&堆優化版的Dijkstra演算法優化
- ban COME IN倉庫管理系統
- java使用PriorityQueue即優先佇列實現大根堆和小根堆Java佇列
- 【Spark篇】---Spark調優之程式碼調優,資料本地化調優,記憶體調優,SparkShuffle調優,Executor的堆外記憶體調優Spark記憶體
- 圖的深度優先遍歷(堆疊實現和非堆疊實現)
- 在SpringBoot中使用logback優化異常堆疊的輸出Spring Boot優化
- 二叉堆實現優先佇列佇列
- 手擼優先佇列——二叉堆佇列
- 面向軌交資訊化,領存FT2000-4 COME核心板亮相