zoukankan      html  css  js  c++  java
  • [CF25C] Roads in Berland

    [CF25C] Roads in Berland - 最短路

    Description

    给定一张无向图,现在知道了任意两个城市之间的最短路径,现在有 k 个询问,想知道每新增一条道路后所有最短路径的总长度。(n<=300, k<=300)

    Solution

    对于每个询问,我们暴力枚举一遍所有点对 i,j,比较 (d(i,j))(d(i,a)+c+d(b,j)) 以及 (d(i,b)+c+d(a,j)),如果小于就更新

    #include <bits/stdc++.h>
    using namespace std;
    
    #define int long long
    
    const int N = 305;
    
    int n, k, d[N][N], a, b, c;
    
    signed main()
    {
        ios::sync_with_stdio(false);
    
        cin >> n;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                cin >> d[i][j];
    
        int total_dist = 0;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j < i; j++)
                total_dist += d[i][j];
    
        cin >> k;
        for (int t = 1; t <= k; t++)
        {
            cin >> a >> b >> c;
            int dist = total_dist;
            for (int i = 1; i <= n; i++)
                for (int j = 1; j < i; j++)
                {
                    int original = d[i][j];
                    int cross = min(d[i][a] + c + d[b][j], d[i][b] + c + d[a][j]);
                    if (cross < original)
                        dist += cross - original, d[i][j] += cross - original, d[j][i] += cross - original;
                }
            cout << dist << " ";
            total_dist = dist;
        }
    }
    
  • 相关阅读:
    CodeForces 385D: Bear and Floodlight
    UVA
    SGU 495: Kids and Prizes
    CodeForces 148D: Bag of mice
    HDU 4405: Aeroplane chess
    HDU 4336: Card Collector
    UVA
    POJ 2577: Interpreter
    伪类选择器 伪原色选择器 选择器的优先级
    复习html CSS选择器 组合选择器和属性选择器
  • 原文地址:https://www.cnblogs.com/mollnn/p/14403806.html
Copyright © 2011-2022 走看看