求调 | dfs | 30pts WA
  • 板块P1396 营救
  • 楼主YMnRb
  • 当前回复3
  • 已保存回复3
  • 发布时间2023/11/29 20:31
  • 上次更新2023/11/29 22:44:59
查看原帖
求调 | dfs | 30pts WA
812740
YMnRb楼主2023/11/29 20:31

C++

Code:

#include <iostream>
using namespace std;

namespace cmp {
    template<typename _Tp> inline _Tp min(const _Tp& a, const _Tp& b) { return a < b ? a : b; }
    template<typename _Tp> inline _Tp max(const _Tp& a, const _Tp& b) { return a > b ? a : b; }
}
struct edge { int u, v, w, nxt; } e[20004];
struct node { int fstedge = -1, ans; bool vis; } nd[10004];
int n, m, s, t, u, v, w, fstedge[10004]; 

void addnode(int u, int i) {
	nd[u].fstedge = i;
	nd[u].ans = 0x3fffffff; 
	nd[i].vis = 0;
}
void addedge(int u, int v, int w, int i) {
    if (nd[u].fstedge == -1) addnode(u, i);
    e[i] = (edge){u, v, w, nd[u].fstedge};
    nd[u].fstedge = i;
}
int dfs(int u) {
    if (u == t) return 0;
    if (nd[u].vis) return nd[u].ans;
    nd[u].vis = 1;
    int i = nd[u].fstedge;
    while (true) {
        nd[u].ans = cmp::min(nd[u].ans, cmp::max(e[i].w, (e[i].v)));
        if (e[i].nxt == i) break;
        else i = e[i].nxt;
    }
    return nd[u].ans;
}

int main() {
    cin >> n >> m >> s >> t;
    for (int i = 0; i < n; ++i) fstedge[i] = -1;
    while (m--) {
        cin >> u >> v >> w;
        addedge(u, v, w, m);
        addedge(v, u, w, m);
    }
    cout << dfs(s);
    return 0;
}
2023/11/29 20:31
加载中...