POJ 1797Heavy Transportation Dijkstra变形与优化
来源:互联网 发布:淘宝客户信誉查询 编辑:程序博客网 时间:2024/06/05 14:18
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.
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.
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.
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.
13 31 2 31 3 42 3 5
Scenario #1:4#include<stdio.h>#include<string.h>#include<algorithm>#include<time.h>#include<stdlib.h>#include<iostream>using namespace std;const int maxn=1050;const int inf=0x3f3f3f3f;int map[maxn][maxn];int book[maxn*100];int weigh[maxn*100];int terms;int points,edges;int a,b,c;int Dijkstra(int start,int end){for(int i=1;i<=points;i++)weigh[i]=map[start][i];book[1]=1;for(int i=1;i<=points;i++){int max1=-1,max_index;for(int j=1;j<=points;j++)if(weigh[j]>max1 && book[j]==0)max1=weigh[j],max_index=j;book[max_index]=1;for(int j=1;j<=points;j++){if(weigh[j] < min(max1,map[max_index][j]) && book[j]==0)weigh[j]=min(max1,map[max_index][j]);}}return weigh[points];}int main(){scanf("%d",&terms);for(int i=1;i<=terms;i++){memset(book,0,sizeof(book));memset(map,0,sizeof(map));memset(weigh,0,sizeof(weigh));scanf("%d%d",&points,&edges);for(int j=1;j<=edges;j++){scanf("%d%d%d",&a,&b,&c);if(c>map[a][b])map[b][a]=map[a][b]=c;}int res=Dijkstra(1,points);printf("Scenario #%d:\n",i);printf("%d\n\n",res);}return 0;}
优先队列优化写法(说实话我没看懂...)还是先看个模板再回顾吧
#include <cstdio>#include <vector>#include <iostream>#include <algorithm>#include <queue>using namespace std;int n,m;vector< pair<int,int> > G[1005];struct cmp{bool operator()( pair<int,int> p1, pair<int,int> p2){return p1.second < p2.second;}};priority_queue< pair<int,int>,vector< pair<int,int> >, cmp > pq;bool vis[1005];int ans[1005];int dijkstra(){while(!pq.empty())pq.pop();pq.push( make_pair(1,9999999) );pair<int,int> tmp;while(!pq.empty()){tmp = pq.top();pq.pop();if( tmp.first==n )return tmp.second;if( vis[tmp.first]==true )continue;ans[tmp.first] = tmp.second;vis[tmp.first] = true;for( vector< pair<int,int> >::iterator it=G[tmp.first].begin(); it!=G[tmp.first].end(); it++ ){if( !vis[it->first] && ans[it->first]<tmp.second && ans[it->first]<it->second ){ans[it->first] = min(tmp.second,it->second);pq.push( make_pair(it->first,ans[it->first]) );}}}}int main(){int t;cin >> t;int start,end,weight;int cas = 0;while(t--){for(int i=0; i<=1000; i++){vis[i] = false;ans[i] = -1;G[i].clear();}cin >> n >> m;for(int i=1; i<=m; i++){scanf("%d %d %d",&start,&end,&weight);G[start].push_back( make_pair(end,weight) );G[end].push_back( make_pair(start,weight) );}int Ans = dijkstra();printf("Scenario #%d:\n",++cas);cout << Ans << endl << endl;}return 0;}
阅读全文
0 0
- POJ 1797Heavy Transportation Dijkstra变形与优化
- POJ 1797 Heavy Transportation(dijkstra变形)
- Poj 1797 Heavy Transportation【Dijkstra变形】
- POJ 1797 - Heavy Transportation(dijkstra变形)
- POJ 1797 Heavy Transportation dijkstra 变形
- poj 1797 Heavy Transportation 【dijkstra的变形】
- POJ.1797 Heavy Transportation (Dijkstra变形)
- POJ 1797 Heavy Transportation(Dijkstra变形) 解题报告
- 7_4_O题 Heavy Transportation 题解[POJ 1797](Dijkstra变形)
- POJ 1797 Heavy Transportation【Dijkstra最短路变形】
- POJ 1797 Heavy Transportation 【dijkstra】
- poj 1797 Heavy Transportation(dijkstra)
- POJ - 1797----Heavy Transportation(Dijkstra)
- POJ 1797Heavy Transportation(dijkstra算法+优先队列优化)
- poj1797 Heavy Transportation dijkstra变形
- poj 1797 Heavy Transportation (最短路变形)
- POJ 1797 Heavy Transportation 最短路变形
- Poj 1797 Heavy Transportation ( 最短路变形
- 设计模式之抽象工厂
- 算法入门经典第三章习题(2)
- poj3268
- 通俗易懂看RB-tree(二)
- vsftpd配置文件详解
- POJ 1797Heavy Transportation Dijkstra变形与优化
- 国外15种手机游戏引擎和开发工具介绍
- 面试OR笔试17——最小排列数
- dbms_stats.set_system_stats 使用
- Android Api demo系列(9) (App>Activity>SecureDialog)
- 微信开发入门
- python实现-数字颠倒
- 搜狐2017 WEB前端开发 秋招笔试编程题
- BZOJ1003物流运输