POJ1797 Heavy Transportation Dijkstra算法

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

题意:给出N个城市M条道路,每条道路都有最大载重,求一条线路使城市1到城市N运输重量最大。

分析:本题对Dijkstra算法的松弛条件进行变形,d[i]记录源点到点i的所有路径中的最大载重。求经过k点到其余点j的最大载重时,由d[k]和k->j权值的较小值来决定是否更新d[j]。

①采用邻接矩阵实现

//Memory 4076K	Time 422MS
#include<iostream>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=1000;
int N,M,weight[maxn][maxn],d[maxn]; 
bool vis[maxn];

void dij(int src)
{
	for(int i=0;i<N;i++)//初始化为源点到各点的路径最大载重 
		d[i]=weight[src][i];
	vis[src]=true;
	for(int i=0;i<N-1;i++)
	{
		int maxWei=-1,k=-1;
		for(int j=0;j<N;j++)//找到未确定的载重最大的点 
			if(!vis[j]&&d[j]>maxWei)
			{
				maxWei=d[j],k=j;
			}	
		if(k==N-1) return;//目的点N已经确定最大承受重量 
		vis[k]=true;
		for(int j=0;j<N;j++)//更新经k点的路径最大承重
			d[j]=max(d[j],min(d[k],weight[k][j]));
	}	
}
int main()
{
	int T,t=1;
	cin>>T; 
	while(T--)
	{
		cin>>N>>M;
		mem(weight,-1);
		mem(vis,false);
		for(int i=0;i<M;i++)
		{
			int v,u,w;
			scanf("%d%d%d",&v,&u,&w);
			weight[v-1][u-1]=weight[u-1][v-1]=w;
		}
		dij(0);
		printf("Scenario #%d:\n%d\n\n",t++,d[N-1]);
	} 
	return 0;
}

 

②邻接表加优先队列实现:

//Memory 1500K	Time 282MS
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<utility>
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=1002;
int N,M,d[maxn]; 
struct Edge{
	int to,weight;
	Edge(int to,int w):to(to),weight(w){	}
}; 
vector<Edge>G[maxn]; //记录邻接的边 

typedef pair<int,int> P;
void dij(int src)
{
	priority_queue<P> que;
	mem(d,0);
	d[src]=INF; //源点设为无穷大 
	que.push(P(d[src],src)); 
	while(!que.empty())
	{
		P p=que.top();que.pop();
		int v=p.second;	
		if(v==N) return;//目的点N已经确定最大承受重量 
		if(d[v]>p.first) continue;//该点已经确定 
		for(int j=0;j<G[v].size();j++)
		{//遍历边 
			Edge e=G[v][j];
			if(min(d[v],e.weight)>d[e.to])
			{//经v点的路径最大承重更大则更新 
				d[e.to]=min(d[v],e.weight);
				que.push(P(d[e.to],e.to));				
			}
		}
			
	}	
}
int main()
{
	int T,t=1;
	cin>>T; 
	while(T--)
	{
		for(int i=0;i<N;i++)
			G[i].clear();
		cin>>N>>M;
		for(int i=0;i<M;i++)
		{
			int v,u,w;
			scanf("%d%d%d",&v,&u,&w);
			//添加两条边 
			G[v].push_back(Edge(u,w));
			G[u].push_back(Edge(v,w));
		}
		dij(1);
		printf("Scenario #%d:\n%d\n\n",t++,d[N]);
	} 
	return 0;
}

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值