我是靠谱客的博主 细心自行车,最近开发中收集的这篇文章主要介绍POJ - 1797 Heavy Transportation (最短路dijkstra),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

https://vjudge.net/problem/POJ-1797

POJ - 1797 Heavy Transportation 最短路dijkstra

      • 题目
      • 分析
      • AC代码 spfa
      • AC代码dijstra

题目

Background
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.

Problem
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo’s place) to crossing n (the customer’s place). You may assume that there is at least one path. All streets can be travelled in both directions.
Input
The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.
Output
The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.
Sample Input
1
3 3
1 2 3
1 3 4
2 3 5
Sample Output
Scenario #1:
4

分析

在这里插入图片描述

AC代码 spfa

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int N=1e6;
int n,m,t,idx;
int dis[N],bk[N];
int h[N],e[N],w[N],ne[N];
void add(int a,int b,int c)
{
e[idx]=b;
w[idx]=c;
ne[idx]=h[a];
h[a]=idx++;
}
void spfa()
{
memset(bk,0,sizeof bk);
memset(dis,0,sizeof dis);
dis[1]=0x3f3f3f3f;
queue<int> q;
q.push(1);
bk[1]=1;
while(q.size())
{
int t=q.front();
q.pop();
bk[t]=0;
for(int i=h[t]; i!=-1; i=ne[i])
{
int j=e[i];
if(dis[j]<dis[t] && dis[j]<w[i])
{
dis[j]=min(dis[t],w[i]);
if(!bk[j])
{
q.push(j);
bk[j]=1;
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>t;
int cnt=1;
while(t--)
{
cin>>n>>m;
int a,b,c;
memset(h,-1,sizeof h);
idx=0;
while(m--)
{
cin>>a>>b>>c;
add(a,b,c);
add(b,a,c);
}
spfa();
cout<<"Scenario #"<<cnt<<":n";
cnt++;
cout<<dis[n]<<"nn";
}
return 0;
}

AC代码dijstra

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1010;
int n,m,t;
int g[N][N],dis[N],bk[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>t;
int cnt=1;
while(t--)
{
cin>>n>>m;
int a,b,c;
memset(g,0,sizeof g);
while(m--)
{
cin>>a>>b>>c;
if(g[a][b]<c)
g[a][b]=c;
if(g[b][a]<g[a][b])
g[b][a]=g[a][b];
}
memset(bk,0,sizeof bk);
memset(dis,0,sizeof dis);
dis[1]=0x3f3f3f3f;
for(int i=0;i<n-1;i++)
{
int t=-1;
for(int j=1;j<=n;j++)
{
if(!bk[j] && (t==-1 || dis[t]<dis[j]))
t=j;
}
bk[t]=1;
for(int j=1;j<=n;j++)
{
if(dis[t]>dis[j] && g[t][j]>dis[j])
dis[j]=min(dis[t],g[t][j]);
}
}
cout<<"Scenario #"<<cnt<<":n";
cnt++;
cout<<dis[n]<<"nn";
}
return 0;
}

最后

以上就是细心自行车为你收集整理的POJ - 1797 Heavy Transportation (最短路dijkstra)的全部内容,希望文章能够帮你解决POJ - 1797 Heavy Transportation (最短路dijkstra)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部