求路径中最小值的最大值,应该可以利用网络流的方法解决,不过这道题就利用了dijkstra的方法解决了。
此前POJ 2253利用Floyd方法解决的思路应该也可以应用到这种方法上来
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
const int maxn= 1005;
const int INF= 0x3f3f3f3f;
struct Edge
{
int b, e, d;
};
struct HeapNode
{
int u, d;
bool operator < (const HeapNode &rhs) const
{
return this->d< rhs.d;
}
};
int n, m;
int vis[maxn], dis[maxn];
vector<Edge> edges;
vector<int> G[maxn];
void Init()
{
edges.clear();
for (int i= 1; i<= n; ++i){
G[i].clear();
}
}
void AddEdge(int &b, int &e, int &w)
{
edges.push_back((Edge){b, e, w});
int se= edges.size();
G[b].push_back(se-1);
}
inline void uAddEdge(int &b, int &e, int &w)
{
AddEdge(b, e, w);
AddEdge(e, b, w);
}
void Dijkstra(int s)
{
priority_queue<HeapNode> Q;
memset(vis, 0, sizeof(vis));
memset(dis, -1, sizeof(dis));
dis[s]= INF;
Q.push((HeapNode){s, 0});
while (!Q.empty()){
HeapNode cur= Q.top();
Q.pop();
int u= cur.u;
if (vis[u]){
continue;
}
vis[u]= 1;
for (vector<int>::iterator g_iter= G[u].begin(); G[u].end()!= g_iter; ++g_iter){
Edge &eg= edges[*g_iter];
int d= min(dis[u], eg.d);
if (d> dis[eg.e]){
dis[eg.e]= d;
Q.push((HeapNode){eg.e, d});
}
}
}
}
int main(int argc, char const *argv[])
{
int kase= 0, sc;
scanf("%d", &sc);
while (sc--){
int b, e, w;
Init();
scanf("%d %d", &n, &m);
for (int i= 0; i< m; ++i){
scanf("%d %d %d", &b, &e, &w);
uAddEdge(b, e, w);
}
Dijkstra(1);
printf("Scenario #%d:\n", ++kase);
printf("%d\n\n", dis[n]);
}
return 0;
}