UVALive 6622 Absurdistan Roads

Source: Internet
Author: User

UVALive 6622 Absurdistan Roads

Question:

The graph of n (2000) points shows its shortest path matrix. A graph with n edges meeting the Shortest Path matrix is constructed to ensure that the graph is connected and the solution exists.

Ideas:

We can first ensure that graph connectivity requires n-1 sides to associate them with the Minimum Spanning Tree ??

It can be assumed that the abc point has been connected, and now it is considered to be added to a point in the connected block. For example, if d-B is the shortest distance from d to abc, then this edge must be if you do not select d-B, if you choose d-a, then d-a is longer than d-B, so the distance between d-B will never be satisfied.

In this way, we can select n-1 Edge Based on the Minimum Spanning Tree. Then we want to know if the Shortest Path matrix is satisfied. If we can add a duplicate edge, we can use dfs. to calculate the shortest path of N-1 edges, because Floyd requires n ^ 3)

If not, what side should we add ?? It is easy to think that the edge with the smallest difference between the new matrix and the original matrix is the proof to be added. It is similar to the above. If we do not add the smallest value, even if we add the smallest value, the smallest value will not be satisfied.

Code:

#include
 
  #include
  
   #include
   
    #include
    
     #include#include
     #include
      
       #include
       
        #include
        
         #include
         
          #include
          
           #include
           
            using namespace std;#define N 2010#define inf (1<<30)int n, first = 1;int maz[N][N], dis[N], vis[N], link[N];int f[N][N];int head[N], tot;struct edge {int u, v, w, next;} ed[N * 2];void add(int u, int v, int w) {ed[tot].u = u;ed[tot].v = v;ed[tot].w = w;ed[tot].next = head[u];head[u] = tot++;}void dfs(int now, int start, int len) {for (int i = head[now]; ~i; i = ed[i].next) {int v = ed[i].v;if (!vis[v]) {vis[v] = 1;f[start][v] = len + ed[i].w;dfs(v, start, f[start][v]);}}}int main() {while (~scanf("%d", &n)) {if (first)first = 0;elseputs("");for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++)scanf("%d", &maz[i][j]);}for (int i = 1; i <= n; i++) {vis[i] = 0;dis[i] = inf;link[i] = -1;head[i] = -1;}tot = 0;dis[1] = 0;memset(f, 0, sizeof(f));for (int i = 1; i <= n; i++) {int pos, near = inf;for (int j = 1; j <= n; j++)if (!vis[j] && near > dis[j]) {pos = j;near = dis[j];}if (link[pos] != -1) {printf("%d %d %d\n", link[pos], pos, maz[pos][link[pos]]);add(pos, link[pos], maz[pos][link[pos]]);add(link[pos], pos, maz[pos][link[pos]]);}vis[pos] = 1;for (int j = 1; j <= n; j++) {if (!vis[j] && dis[j] > maz[pos][j]) {link[j] = pos;dis[j] = maz[pos][j];}}}int a, b, c = inf;for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++)vis[j] = 0;vis[i] = 1;dfs(i, i, 0);for (int j = 1; j <= n; j++) {if (maz[i][j] < f[i][j] && c > maz[i][j]) {a = i;b = j;c = maz[i][j];}}}if (c == inf)a = ed[0].u, b = ed[0].v, c = ed[0].w;printf("%d %d %d\n", a, b, c);}return 0;}
           
          
         
        
       
      
    
   
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.