zoukankan      html  css  js  c++  java
  • 据说是Flord算法

    贵有恒,何必三更起五更眠;最无益,莫过一日曝十日寒。 问题 C: Restoring Road Network

    问题 C: Restoring Road Network

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 896  解决: 184
    [提交] [状态] [讨论版] [命题人:admin]

    题目描述

    In Takahashi Kingdom, which once existed, there are N cities, and some pairs of cities are connected bidirectionally by roads. The following are known about the road network:
    People traveled between cities only through roads. It was possible to reach any city from any other city, via intermediate cities if necessary.
    Different roads may have had different lengths, but all the lengths were positive integers.
    Snuke the archeologist found a table with N rows and N columns, A, in the ruin of Takahashi Kingdom. He thought that it represented the shortest distances between the cities along the roads in the kingdom.
    Determine whether there exists a road network such that for each u and v, the integer Au,v at the u-th row and v-th column of A is equal to the length of the shortest path from City u to City v. If such a network exist, find the shortest possible total length of the roads.

    Constraints
    1≤N≤300
    If i≠j, 1≤Ai,j=Aj,i≤109.
    Ai,i=0


    输入

    Input is given from Standard Input in the following format:
    N
    A1,1 A1,2 … A1,N
    A2,1 A2,2 … A2,N

    AN,1 AN,2 … AN,N


    输出

    If there exists no network that satisfies the condition, print -1. If it exists, print the shortest possible total length of the roads.

    样例输入

    3
    0 1 3
    1 0 2
    3 2 0
    

    样例输出

    3
    

    提示

    The network below satisfies the condition:
    City 1 and City 2 is connected by a road of length 1.
    City 2 and City 3 is connected by a road of length 2.
    City 3 and City 1 is not connected by a road.


    [提交][状态]

    这个题目一开始没读懂,明明是最短路了为啥还要最短路,赛后查题解才知道,原来是需要你进行判断。。。。
    自己为什么这么菜。注意 he thought !!!!
    判断的时候 如果 经过一个额外的一点的距离小于所给的数据,那么他就不是最短距离。


    #include <iostream>
    using namespace std;
    int a[310][310];
    int main(){
        int n;
        cin>>n;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cin>>a[i][j];
            }
        }
    
        int flag=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                for(int k=0;k<n;k++){
                    if(a[i][k]+a[k][j]<a[i][j]&&i!=j&&j!=k&&i!=k){
                        flag=1;
                        break;
                    } 
                    if(a[i][k]+a[k][j]==a[i][j]&&i!=j&&j!=k&&i!=k){
                        a[i][k]=0;//这里不应该赋值为0,会影响之后的判断。
                        a[k][i]=0;
                    }
                }
                if(flag) {
                    break;
                } 
            }
            if(flag){
                break;
            }
        }
        if(flag){
            cout<<-1;
        }
        else{
            int sum=0;
            for(int i=0;i<n;i++){
                for(int j=i+1;j<n;j++){
                    sum+=a[i][j];
    
    
    
        +1;j<n;j++){
                if(v[i][j]!=1){`}
                sum+=a[i][j];
                } 
            }
    }
        cout<<sum;
    }
                } 
            }
        }
        cout<<sum;
  • 相关阅读:
    Docker磁盘垃圾清理
    什么是容器编排?
    Docker 容器连接
    docker入门操作整理
    Docker学习的几个建议和技巧
    支付清结算之电商侧处理
    在Linux 中进入单用户模式的技巧
    教你如何构建异步服务器和客户端的 Kotlin 框架 Ktor
    NetSuite助力各行业企业快速发展
    linux需要你的不懈努力
  • 原文地址:https://www.cnblogs.com/bianzhuo/p/9463280.html
Copyright © 2011-2022 走看看