ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POJ - 1797 Heavy Transportation (最短路dijstra)

2021-07-24 10:00:25  阅读:215  来源: 互联网

标签:Heavy customer Transportation weight int streets 1797 cin dis


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

POJ - 1797 Heavy Transportation 最短路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代码

#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]<<"\n\n";
	}
	return 0;
}

标签:Heavy,customer,Transportation,weight,int,streets,1797,cin,dis
来源: https://blog.csdn.net/weixin_52341477/article/details/119053222

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有