首页 > 其他 > 详细

C. Roads in Berland

时间:2019-07-27 17:34:02      阅读:147      评论:0      收藏:0      [点我收藏+]

题目链接:

http://codeforces.com/problemset/problem/25/C

题意:

给一个最初的所有点与点之间的最短距离的矩阵。然后向图里加边,原有的边不变,问加边后的各个顶点的距离是多少。

思路:

这个一看就知道是folyd的变种,关键是状态转移怎么处理,具体看代码。

代码:

技术分享图片
#include<bits/stdc++.h>
#define LL long long

using namespace std;
const int maxn=500;
int dist[maxn][maxn];
LL ans;
void modify(int u,int v,int w)
{
    if(dist[u][v]>w)
    {
        ans-=(dist[v][u]-w);
        dist[u][v]=w;
    }
    return;
}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cin>>dist[i][j];
            if(j>i)
            {
                ans+=dist[i][j];
            }
        }

    }
    //cout<<ans<<endl;
    int k;
    cin>>k;
    for(int i=1;i<=k;i++)
    {
        int u,v,w;
        cin>>u>>v>>w;
        modify(u,v,w);
        if(dist[u][v]!=dist[v][u])
        {
            dist[v][u]=dist[u][v];
        }
        //cout<<ans<<endl;
        for(int p=1;p<=n;p++)
        {
            for(int q=1;q<=n;q++)
            {
                modify(p,q,dist[p][u]+dist[v][q]+w);
                if(dist[p][q]!=dist[q][p])
                {
                    dist[q][p]=dist[p][u]+dist[v][q]+w;
                }
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}
View Code

 

 

C. Roads in Berland

原文:https://www.cnblogs.com/dx666/p/dxnb666.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!