首页 > 其他分享 >abc051 <多源最短路>

abc051 <多源最短路>

时间:2023-06-21 10:33:23浏览次数:47  
标签:typedef contests int tasks abc051 include

https://atcoder.jp/contests/abc051/tasks/abc051_d

// https://atcoder.jp/contests/abc051/tasks/abc051_d
// 一条边不含于任何一条最短路中, 当且仅当w[i][j] > dist[i][j], 即存在一条最短路的权比这条边的权小

#include <iostream>
#include <algorithm>
#include <cstring>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 110, INF = 0x3f3f3f3f;
int g[N][N];  // Floyd
int f[N][N];  // 边权备份
int n, m;

void solv()
{
    cin >> n >> m;
    int a, b, c;
    // 初始化
    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= n; j ++)
            if (i != j) g[i][j] = INF;
    // 读入
    for (int i = 1; i <= m; i ++)
    {
        cin >> a >> b >> c;
        g[a][b] = g[b][a] = c;
    }
    // 备份边权
    memcpy(f, g, sizeof f);

    // floyed
    for (int k = 1; k <= n; k ++)
        for (int i = 1; i <= n; i ++)
            for (int j = 1; j <= n; j ++)
                if (g[i][k] + g[k][j] < g[i][j]) g[i][j] = g[i][k] + g[k][j];
    // count
    int cnt = 0;
    for (int i = 1; i <= n; i ++)
        for (int j = i + 1; j <= n; j ++)
        {
            // cout << i << ',' << j << "   " << g[i][j] << f[i][j] << endl;
            if (f[i][j] < INF && f[i][j] > g[i][j]) cnt ++;
        }
    cout << cnt << endl;

}

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int T = 1;
    // cin >> T;
    while (T --)
    {
        solv();
    }
    return 0;
}

标签:typedef,contests,int,tasks,abc051,include
From: https://www.cnblogs.com/o2iginal/p/17495605.html

相关文章

  • AT2282 [ABC051C] Back and Forth 题解
    Description在一个平面直角坐标系内,有一点\(A(x_1,y_1)\)和点\(B(x_2,y_2)\)你需要从\(A\)点走到\(B\)点,再走到\(A\)点,再走到\(B\)点,再回到\(A\)点。期间,你......