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
Output
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;
}