zoukankan      html  css  js  c++  java
  • bzoj 1880 最短路径图

    #include<bits/stdc++.h>
    #define LL long long
    #define fi first
    #define se second
    #define mk make_pair
    #define PII pair<int, int>
    #define y1 skldjfskldjg
    #define y2 skldfjsklejg
    
    using namespace std;
    
    const int N = 1500 + 7;
    const int inf = 0x3f3f3f3f;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const int mod = 1e9 + 7;
    
    int n, m, s1, t1, s2, t2, tot, head[N], d[N][N], dp[N];
    vector<PII> G[N];
    
    struct Edge {
        int from, to, w, nx;
    } edge[2000007];
    
    void add(int u, int v, int w) {
        edge[tot].from = u;
        edge[tot].to = v;
        edge[tot].w = w;
        edge[tot].nx = head[u];
        head[u] = tot++;
    }
    
    void Dij(int S, int *d) {
        priority_queue<PII, vector<PII>, greater<PII> > que;
        que.push(mk(0, S)), d[S] = 0;
        while(!que.empty()) {
            PII now = que.top(); que.pop();
            int u = now.se, dis = now.fi;
            if(dis > d[u]) continue;
            for(int i = head[u]; ~i; i = edge[i].nx) {
                int v = edge[i].to, w = edge[i].w;
                if(dis + w < d[v]) {
                    d[v] = dis + w;
                    que.push(mk(d[v], v));
                }
            }
        }
    }
    
    int dfs(int u) {
        if(~dp[u]) return dp[u];
        dp[u] = 0;
        for(int i = 0; i < G[u].size(); i++) {
            int v = G[u][i].se, w = G[u][i].fi;
            dp[u] = max(dp[u], dfs(v) + w);
        }
        return dp[u];
    }
    
    int cal(int s1, int t1, int s2, int t2) {
        memset(dp, -1, sizeof(dp));
        for(int i = 1; i <= n; i++) G[i].clear();
        for(int i = 0; i < tot; i++) {
            int u = edge[i].from, v = edge[i].to, w = edge[i].w, f = 0;
            if(d[s1][u] + d[t1][v] + w == d[s1][t1]) f++;
            if(d[s2][u] + d[t2][v] + w == d[s2][t2]) f++;
            if(f == 2) {
                G[u].push_back(mk(w, v));
            }
        }
        int ans = 0;
        for(int i = 1; i <= n; i++)
            ans = max(ans, dfs(i));
        return ans;
    }
    
    int main() {
        memset(head, -1, sizeof(head));
        memset(d, inf, sizeof(d));
        scanf("%d%d", &n, &m);
        scanf("%d%d%d%d", &s1, &t1, &s2, &t2);
        for(int i = 1; i <= m; i++) {
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            add(u, v, w); add(v, u, w);
        }
    
        Dij(s1, d[s1]);
        Dij(t1, d[t1]);
        Dij(s2, d[s2]);
        Dij(t2, d[t2]);
    
        int ans = 0;
        ans = max(ans, cal(s1, t1, s2, t2));
        ans = max(ans, cal(s1, t1, t2, s2));
    
        printf("%d
    ", ans);
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    onkeyup事件
    asp.net学习视频资料地址链接
    企业站新闻信息的后台功能开发心得(1)
    js实现切换导航
    jquery实现上拉加载更多
    css设置input框没有边框,选中时取消背景色
    使用js修改url地址参数
    ubantu 修改hosts文件
    git的使用
    ubantu 16.04 菜单栏的位置变换
  • 原文地址:https://www.cnblogs.com/CJLHY/p/9648385.html
Copyright © 2011-2022 走看看