POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)

Description

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

Http

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

SCU:https://vjudge.net/problem/SCU-1819

Source

图论,最短路径

题目大意

求解两点之间所有路径中,最小权值最大的路径

解决思路

同样运用改进的spfa算法解决,顺带复习了一下Dijkstra算法

Dijkstra(250ms)略快于spfa(266ms)

请不要使用cin读入,并且注意输出格式

代码

spfa实现

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std; const int maxN=1001;
const int inf=2147483647; class Edge
{
public:
int v,w;
}; int n,m;
vector<Edge> E[maxN];
int Dist[maxN];
bool inqueue[maxN];
queue<int> Q; int main()
{
int T;
cin>>T;
for (int ti=1;ti<=T;ti++)
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
E[i].clear();
for (int i=1;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
E[u].push_back((Edge){v,w});
E[v].push_back((Edge){u,w});
}
memset(Dist,0,sizeof(Dist));
memset(inqueue,0,sizeof(inqueue));
while (!Q.empty())
Q.pop();
inqueue[1]=1;
Q.push(1);
Dist[1]=inf;
do
{
int u=Q.front();
Q.pop();
inqueue[u]=0;
for (int i=0;i<E[u].size();i++)
{
int v=E[u][i].v;
if (min(Dist[u],E[u][i].w)>Dist[v])
{
Dist[v]=min(Dist[u],E[u][i].w);
if (inqueue[v]==0)
{
Q.push(v);
inqueue[v]=1;
}
}
}
}
while (!Q.empty());
printf("Scenario #%d:\n%d\n\n",ti,Dist[n]);
}
return 0;
}

Dijkstra实现

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std; const int maxN=1001;
const int inf=2147483647; class Edge
{
public:
int v,w;
}; int n,m;
vector<Edge> E[maxN];
int Dist[maxN];
bool vis[maxN]; int main()
{
int T;
cin>>T;
for (int ti=1;ti<=T;ti++)
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
E[i].clear();
for (int i=1;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w); E[u].push_back((Edge){v,w});
E[v].push_back((Edge){u,w});
}
memset(Dist,0,sizeof(Dist));
memset(vis,0,sizeof(vis));
Dist[1]=inf;
for (int i=1;i<n;i++)
{
int id,mx=-inf;
for (int j=1;j<=n;j++)
if ((Dist[j]>mx)&&(vis[j]==0))
{
mx=Dist[j];
id=j;
}
if (id==n)
break;
vis[id]=1;
for (int j=0;j<E[id].size();j++)
{
int v=E[id][j].v;
if ((vis[v]==0)&&(min(Dist[id],E[id][j].w)>Dist[v]))
{
Dist[v]=min(Dist[id],E[id][j].w);
}
}
}
printf("Scenario #%d:\n%d\n\n",ti,Dist[n]);
}
return 0;
}

POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)的更多相关文章

  1. POJ 1797 Heavy Transportation (Dijkstra变形)

    F - Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  2. poj 1797 Heavy Transportation(最大生成树)

    poj 1797 Heavy Transportation Description Background Hugo Heavy is happy. After the breakdown of the ...

  3. POJ.1797 Heavy Transportation (Dijkstra变形)

    POJ.1797 Heavy Transportation (Dijkstra变形) 题意分析 给出n个点,m条边的城市网络,其中 x y d 代表由x到y(或由y到x)的公路所能承受的最大重量为d, ...

  4. POJ 1797 ——Heavy Transportation——————【最短路、Dijkstra、最短边最大化】

    Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64 ...

  5. Heavy Transportation POJ 1797 最短路变形

    Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...

  6. POJ 1797 Heavy Transportation (最大生成树)

    题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...

  7. POJ 1797 Heavy Transportation (Dijkstra)

    题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...

  8. POJ 2502 Subway / NBUT 1440 Subway / SCU 2186 Subway(图论,最短距离)

    POJ 2502 Subway / NBUT 1440 Subway / SCU 2186 Subway(图论,最短距离) Description You have just moved from a ...

  9. POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)

    POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...

随机推荐

  1. Maltego——互联网情报聚合工具初探(转)

    有时候你可曾想过,从一个Email,或者Twitter,或是网站,甚至姓名等等,能找到一个人千丝万缕的联系,并把这些联系整合,利用起来?Maltego就是这样一款优秀而强大的工具.Maltego允许从 ...

  2. 赚钱的小生意,VC对你没兴趣

    创业者,赚钱的生意就不要去找VC(风险投资)了,因为人家对你的生意没有兴趣. 无论是创业者,VC,股权投资散户,都需要对一个"生意"的规模有个总体的认识. 就"生意&qu ...

  3. MFC 用ShellExecute打开外部文件

    知识点: 获取CListCtrl选中文本 用ShellExecute打开外部文件 一.CListCtrl::GetFirstSelectedItemPosition CListCtrl::GetFir ...

  4. flask前端与后端之间传递的两种数据格式:json与FormData

    json格式 双向! 前端 ==>后端:json格式 后端 ==>前端:json格式 html <!-- html部分 --> <form enctype='applic ...

  5. libgdx学习记录10——Particle粒子

    粒子对制作画面特效很有用,可以使用Particle Editor进行自行编辑粒子效果,跟图片一起生成.p粒子文件,然后导入到程序中使用. 本文所用的粒子效果是基于其自带的demo的. 实例: pack ...

  6. 微信小程序之可滚动视图容器组件 scroll-view

    1. 纵向滚动 scroll-y 当 设置为scroll-y 时, 需要将其高度设为固定值 如果整个页面,即最外层标签为scroll-view,需要并将其高度设为100%,也需要将page设为100% ...

  7. [git hooks] pre-commit 配置

    在开发过程中,通常使用 eslint 来规范团队的代码风格.但是 eslint 只能在开发服务器启动的时候才去检验代码.如果一个人在不启动开发服务器的情况下,修改了代码直接提交到git,那么别人pul ...

  8. 机器学习英雄访谈录之 DL 自由职业者:Tuatini Godard

    目录 机器学习英雄访谈录之 DL 自由职业者:Tuatini Godard 正文 对我的启发 机器学习英雄访谈录之 DL 自由职业者:Tuatini Godard Sanyam Bhutani 是 M ...

  9. [文章存档]Azure上部署的java app在向第三方服务传送中文时出现乱码

    https://docs.azure.cn/zh-cn/articles/azure-operations-guide/app-service-web/aog-app-service-web-java ...

  10. PAT甲题题解-1044. Shopping in Mars (25)-水题

    n,m然后给出n个数让你求所有存在的区间[l,r],使得a[l]~a[r]的和为m并且按l的大小顺序输出对应区间.如果不存在和为m的区间段,则输出a[l]~a[r]-m最小的区间段方案. 如果两层fo ...