码迷,mamicode.com
首页 > 其他好文 > 详细

最短路变形2.。

时间:2017-03-11 21:34:57      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:als   win   ane   tree   not   i++   学习   limit   lan   

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


求到达某点路径最小边权中的最大值 分开来看 先看求最大值这个问题 这个问题是和最短路思想一致的 状态转移和青蛙的那道题类似
这里再说下dijstra的算法具体实现 算法的思想是先找一个最短距离确定的点 然后拿这个点去更新它能达到点的距离 不断重复这个过程,
理解这个问题的时候,我卡在了同一个点有不同最短路怎么处理的问题。,。 其实很简单了 先明白一点 新的最短距离确定的点的生成是在更新的时候出现的
那么已经是最短距离的点在更新的时候由于条件的判断 不会造成干扰 不过还是可以再次访问的
上两个代码。

#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct node
{
int x,cost;
};
typedef pair<int,int> p;// first->x second ->到已经确定集合的最短距离  这个的定义要理解清楚 是点到已经处理集合的最值距离!
vector<node> edge[1001];
int n,m;
void dijstra(int s)
{
int d[1001];
fill(d,d+1001,0);
priority_queue<p,vector<p>,less<p> > que;//这里的定义要学习一下
d[s]=1000001;
que.push(p(s,1000001));
while(!que.empty())
{
p temp = que.top();
que.pop();
if(d[temp.first] > temp.second) continue;// 拿出的点不是距离最大 丢掉! 这个很重要 !
for(int i=0;i<edge[temp.first].size();i++)
{
int x=edge[temp.first][i].x,cost=edge[temp.first][i].cost;
if(d[temp.first] > d[x]&&cost>d[x])
{
d[x]=min(d[temp.first],cost);
que.push(p(x,d[x]));
}
}
}
cout<<d[n]<<endl;
cout<<endl;
}
void init()
{
for(int i=1;i<=n;i++) edge[i].clear();
}
int main()
{
cin.sync_with_stdio(false);
int t;
cin>>t;
init();
for(int Case=1;Case<=t;Case++)
{
cin>>n>>m;
node temp;
int x,y;
while(m--)
{
cin>>x>>y>>temp.cost;
temp.x=x;
edge[y].push_back(temp);
temp.x=y;
edge[x].push_back(temp);
}
printf("Scenario #%d:\n",Case);
dijstra(1);
}
return 0;
}

#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct node
{
int x,cost;
};
int mapp[1010][1010];
int n,m;
int min(int x,int y)
{
if(x>y) return y;
else return x;
}
void dijstra(int s)
{
int vis[1010],d[1010];
for(int i=1;i<=n;i++)
{
d[i]=mapp[1][i];
vis[i]=0;
}
for(int i=1;i<=n;i++)
{
int v=-1;
for(int i=1;i<=n;i++)
{
if(!vis[i]&&(d[i]>d[v]||v == -1)) v=i;
}
//if(v==-1) break;
vis[v]=1;
for(int i=1;i<=n;i++)
{
if(!vis[i]&&d[i]<min(mapp[v][i],d[v])) d[i]=min(d[v],mapp[v][i]);
}
}
cout<<d[n]<<endl<<endl;
}
int main()
{
cin.sync_with_stdio(false);
int t;
cin>>t;
memset(mapp,0,sizeof(mapp));
for(int Case=1;Case<=t;Case++)
{
cin>>n>>m;
int x,y,z;
for(int i=1;i<=m;i++)
{
cin>>x>>y>>z;
mapp[x][y]=mapp[y][x]=z;
}
printf("Scenario #%d:\n",Case);
dijstra(1);
}
return 0;
}

 

最短路变形2.。

标签:als   win   ane   tree   not   i++   学习   limit   lan   

原文地址:http://www.cnblogs.com/z1141000271/p/6536078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!