一、题目大意
有N个结点,有m条边,每条边有个承重限制。问从1走到N的所有路中,最大承重路径的承重是多少。
二、解题思路
使用优先队列,队列中存的状态为(place, w)
:表示该路径从1
到place
,且其最大承受重量为w
。通过维护一个max_w[v]
表示目前发现的从1
到v
的所有路径的最大承受重量。以此剪枝
三、代码
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN = 1005;
const int inf = 1 << 30;
struct Node
{
int place, w;
bool operator < (const Node&A) const{
return w < A.w;
}
};
priority_queue<Node> Q;
vector<Node> G[MAXN];
int n, m;
int max_w[MAXN];
void djstla()
{
memset(max_w, 0, sizeof(max_w));
while(!Q.empty())
Q.pop();
max_w[1] = inf;
Q.push((Node){1, inf});
while(!Q.empty())
{
Node top = Q.top(); Q.pop();
if(top.w <= max_w[n])
return;
int v = top.place;
for(int idv=0; idv<G[v].size(); idv++)
{
int to = G[v][idv].place;
int w = min(G[v][idv].w, top.w);
if(w <= max_w[to])
continue;
max_w[to] = w;
Q.push((Node){to, w});
}
}
}
int main()
{
int T;
cin >> T;
for(int k=1; k<=T; k++)
{
cin >> n >> m;
for(int i=0; i<MAXN; i++)
G[i].clear();
int s, t, w;
for(int i=0; i<m; i++)
{
cin >> s >> t >> w;
G[s].push_back((Node){t,w});
G[t].push_back((Node){s,w});
}
djstla();
cout << "Scenario #" << k << ":" << endl;
cout << max_w[n] << endl << endl;
}
return 0;
}