(重边/桥)Caocao's Bridges

http://acm.hdu.edu.cn/showproblem.php?pid=4738

输出图中的桥的最小值
注意重边及当值为0时需要输出1

#include 
using namespace std;
const int maxn = 1000 + 5, maxm = maxn * maxn, inf = 0x3f3f3f3f;
struct Edge {
    int to, nxt, val;
    Edge() {}
    Edge(int _to, int _nxt, int _val) : to(_to), nxt(_nxt), val(_val) {}
} edge[maxm];
int head[maxn], tot;
void add(int u, int v, int w) {
    edge[tot] = Edge(v, head[u], w);
    head[u] = tot++;
}
int dfn[maxn], low[maxn], idx;
int ans;
void Tarjan(int u, int pre) {
    dfn[u] = low[u] = ++idx;
    int k = 0;
    for(int i = head[u]; i != -1; i = edge[i].nxt) {
        int v = edge[i].to;
        if(v == pre && !k) {
            k++;
            continue;
        }
        if(!dfn[v]) {
            Tarjan(v, u);
            if(low[u] > low[v]) low[u] = low[v];
            if(low[v] > dfn[u]) ans = min(ans, edge[i].val);
        }
        else if(low[u] > dfn[v])
            low[u] = dfn[v];
    }
}
void init() {
    tot = idx = 0;
    ans = inf;
    memset(head, -1, sizeof(head));
    memset(dfn, 0, sizeof(dfn));
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    while(cin >> n >> m) {
        if(n == 0 && m == 0) break;
        init();
        int u, v, w;
        for(int i = 0; i < m; ++i) {
            cin >> u >> v >> w;
            add(u, v, w);
            add(v, u, w);
        }
        int block = 0;
        for(int i = 1; i <= n; ++i) {
            if(!dfn[i]) {
                block++;
                Tarjan(i, -1);
            }
        }
        if(block > 1) {
            cout << 0 << endl;
            continue;
        }
        if(ans == 0) cout << 1 << endl;
        else if(ans == inf) cout << -1 << endl;
        else cout << ans << endl;
    }
}

你可能感兴趣的:(总结)