zoukankan      html  css  js  c++  java
  • hdu4034 Graph(floyd)

    题目:  给出一个图的最短路,求原图最少几条边

    Graph

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
    Total Submission(s): 1695    Accepted Submission(s): 848


    Problem Description
    Everyone knows how to calculate the shortest path in a directed graph. In fact, the opposite problem is also easy. Given the length of shortest path between each pair of vertexes, can you find the original graph?
     
    Input
    The first line is the test case number T (T ≤ 100).
    First line of each case is an integer N (1 ≤ N ≤ 100), the number of vertexes.
    Following N lines each contains N integers. All these integers are less than 1000000.
    The jth integer of ith line is the shortest path from vertex i to j.
    The ith element of ith line is always 0. Other elements are all positive.
     
    Output
    For each case, you should output “Case k: ” first, where k indicates the case number and counts from one. Then one integer, the minimum possible edge number in original graph. Output “impossible” if such graph doesn't exist.

     
    Sample Input
    3 3 0 1 1 1 0 1 1 1 0 3 0 1 3 4 0 2 7 3 0 3 0 1 4 1 0 2 4 2 0
     
    Sample Output
    Case 1: 6 Case 2: 4 Case 3: impossible
     
    Source
     
     
     
    先假设每两个点之间都有边,ans = n*(n-1), 然后用floyd , 对于 i,j , 以k为中点,如果 dist[i][j] == dist[i][k]+dist[k][j] 说明这条路可以被替代,减去一条边。
    反之如果大于的话,就和题目的最短路矛盾了。 输出impossible
     
    代码:
     1 #include <iostream>
     2 #include <sstream>
     3 #include <ios>
     4 #include <iomanip>
     5 #include <functional>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <string>
     9 #include <list>
    10 #include <queue>
    11 #include <deque>
    12 #include <stack>
    13 #include <set>
    14 #include <map>
    15 #include <cstdio>
    16 #include <cstdlib>
    17 #include <cmath>
    18 #include <cstring>
    19 #include <climits>
    20 #include <cctype>
    21 using namespace std;
    22 #define XINF INT_MAX
    23 #define INF 0x3FFFFFFF
    24 #define MP(X,Y) make_pair(X,Y)
    25 #define PB(X) push_back(X)
    26 #define REP(X,N) for(int X=0;X<N;X++)
    27 #define REP2(X,L,R) for(int X=L;X<=R;X++)
    28 #define DEP(X,R,L) for(int X=R;X>=L;X--)
    29 #define CLR(A,X) memset(A,X,sizeof(A))
    30 #define IT iterator
    31 typedef long long ll;
    32 typedef pair<int,int> PII;
    33 typedef vector<PII> VII;
    34 typedef vector<int> VI;
    35 
    36 int N;
    37 int dist[105][105];
    38 int vis[105][105];
    39 int main()
    40 {
    41     ios::sync_with_stdio(false);
    42 
    43     int tst;
    44     cin>>tst;
    45     int cse = 0;
    46     while(tst--)
    47     {
    48         CLR(dist,0);CLR(vis,0);
    49 
    50         cin>>N;
    51         REP(i,N)
    52         REP(j,N)
    53         {
    54             cin>>dist[i][j];
    55         }
    56         cout<<"Case "<<++cse<<": ";
    57 
    58         int ans = N*(N-1);
    59         REP(k,N)
    60         {
    61             REP(i,N)
    62             {
    63                 REP(j,N)
    64                 {
    65                     if( i == k || j == k)continue;
    66                     if(!vis[i][j] && dist[i][j] == dist[i][k]+dist[k][j])
    67                     {
    68                         ans--;
    69                         vis[i][j] =1;
    70                     }
    71                     if( dist[i][j] > dist[i][k]+dist[k][j])
    72                     {
    73                         ans = -1;
    74                         break;
    75                     }
    76                 }
    77                 if( ans ==-1)
    78                 {
    79                     break;
    80                 }
    81             }
    82             if(ans==-1)break;
    83         }
    84         if( ans == -1)
    85         {
    86             cout<<"impossible"<<endl;
    87         }
    88         else
    89         {
    90             cout<<ans<<endl;
    91         }
    92 
    93     }
    94 
    95     return 0;
    96 }
    View Code
     
     
  • 相关阅读:
    开发过程中解决各种跨域问题
    使用vux的x-input组件中show-clear=“true”清除icon点击失效的问题
    Vue项目使用域名访问配置
    Taro 压缩图片api
    javascript实现继承的4种方法,以及它们的优缺点
    解决window.opener.obj instanceof Object会输出false的问题
    javascript实现引用数据类型的深拷贝和浅拷贝详解
    javascript检测基本类型值或引用类型值的类型方法
    git merge合并时遇上refusing to merge unrelated histories的解决方案
    vue-router+webpack线上部署时单页项目路由,刷新页面出现404问题
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3790154.html
Copyright © 2011-2022 走看看