当前位置: 代码迷 >> 综合 >> POJ - 1797 Heavy Transportation(最大化最小边)
  详细解决方案

POJ - 1797 Heavy Transportation(最大化最小边)

热度:51   发布时间:2023-12-17 02:52:08.0

最大化最小值是指从一个结点到另一个结点,所能走的所有路径中,选择一条路径的最小边比其他的路径最小边都大。还是在最短路的基础上随便改改就行(相信我,改的太随便就会wa,亲身经历)。

判断条件:d[v] < min(es[i].w, d[u]);

关键式:d[v] = min(es[i].w, d[u]);

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#define inf 0x3f3f3f3fusing namespace std;const int maxn = 1005;int n, m, s, t, T, p = 0;   //n为点数 s为源点
int head[maxn]; //head[from]表示以head为出发点的邻接表表头在数组es中的位置,开始时所有元素初始化为-1
int d[maxn]; //储存到源节点的距离,在Spfa()中初始化
int cnt[maxn];
bool inq[maxn]; //这里inq作inqueue解释会更好,出于习惯使用了inq来命名,在Spfa()中初始化
int nodep;  //在邻接表和指向表头的head数组中定位用的记录指针,开始时初始化为0struct node {int v, w, next;
}es[1000005];void init() {for(int i = 1; i <= n; i++) {d[i] = -inf;inq[i] = false;cnt[i] = 0;head[i] = -1;}nodep = 0;
}void addedge(int from, int to, int weight)
{es[nodep].v = to;es[nodep].w = weight;es[nodep].next = head[from];head[from] = nodep++;
}bool spfa()
{queue<int> que;d[s] = inf;//s为源点inq[s] = 1;que.push(s);while(!que.empty()) {int u = que.front();que.pop();inq[u] = false;   //从queue中退出//遍历邻接表for(int i = head[u]; i != -1; i = es[i].next) {  //在es中,相同from出发指向的顶点为从head[from]开始的一项,逐项使用next寻找下去,直到找到第一个被输//入的项,其next值为-1int v = es[i].v;if(d[v] < min(es[i].w, d[u])) { //松弛(RELAX)操作d[v] = min(es[i].w, d[u]);if(!inq[v]) {      //若被搜索到的节点不在队列que中,则把to加入到队列中去inq[v] = true;que.push(v);}}}}
}int main()
{cin >> T;while(T--) {p++;cin >> n >> m;init();int a, b, c;while(m--) {cin >> a >> b >> c;addedge(a, b, c);addedge(b, a, c);}s = n, t = 1;spfa();printf("Scenario #%d:\n%d\n", p, d[t]);cout << endl;}return 0;
}