[NOIP模拟] 路径统计 floyd

About:

    2017.11.09 T2

Solution:

    这道题的瓶颈在于怎么将点权和边权结合到一起。我们可以先将点权排序,这样我们可以很轻松地找到对应的路径点权最大,对于每次的 floyd 我们可以有这样的式子 :

Edge[i][j]=min(Edge[i][j],max(Edge[i][k],Edge[k][j]))
    这里的 Edge 表示边权,接下来我们想想怎么更新答案首先我们可以得到, 当 k 的编号大于或等于 i 和 j 时, k 所对应的节点为路径上最大值,因为我们是按照点权排序,所以 :
Ans[i][j]=min(Ans[i][j],WkEdge[i][j])
    这时我们考虑,路径上的最大点权在端点上,由于我们 floyd 一定会扫到 k 点在端点上的情况所以我们一定会在更新上种情况中就更新了下一种情况。



Code :

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define LL long long
using namespace std;

inline int read() {
    int i = 0, f = 1;
    char ch = getchar();
    while(!isdigit(ch)) {
        if(ch == '-') f = -1; ch = getchar();
    }
    while(isdigit(ch)) {
        i = (i << 3) + (i << 1) + ch - '0'; ch = getchar();
    }
    return i * f;
}

const int MAXN = 505;
int f[MAXN][MAXN], b[MAXN];
struct point {
    int x, pos;
    inline bool operator < (const point & a) const {
        return x < a.x;
    }
};
point a[MAXN];
LL g[MAXN][MAXN];

int main() {
    memset(f, 0x3f3f3f3f, sizeof(f));
    memset(g, 0x3F, sizeof(g));
    int n = read(), m = read();
    for(register int i = 1; i <= n; ++i) a[i].x = read(), a[i].pos = i, f[i][i] = 0;
    sort(a + 1, a + n + 1);
    for(register int i = 1; i <= n; ++i) b[a[i].pos] = i;
    for(register int i = 1; i <= m; ++i) {
        register int x = read(), y = read();
        f[b[x]][b[y]] = read();
        f[b[y]][b[x]] = f[b[x]][b[y]];
    }
    for(register int k = 1; k <= n; ++k)
        for(register int i = 1; i <= n; ++i) {
            if(f[i][k] == 0x3f3f3f3f) continue; 
            for(register int j = 1; j <= n; ++j) {
                if(f[k][j] == 0x3f3f3f3f) continue;
                f[i][j] = min(f[i][j], max(f[i][k], f[k][j]));
                if(f[i][j] <= 0x3f3f3f3f && i <= k && j <= k)
                    g[i][j] = min(g[i][j], (LL)a[k].x * f[i][j]);
            }
        }
    for(register int i = 1; i <= n; ++i)
        for(register int j = 1; j <= n; ++j)
            if(f[i][j] == 0x3f3f3f3f) g[i][j] = -1;
    for(register int i = 1; i <= n; ++i) {
        for(register int j = 1; j <= n; ++j)
            cout<' ';
        putchar('\n');
    }
}

你可能感兴趣的:(NOIP模拟,floyd,floyd的关键运用)