SPFA - P3905 road reconstruction

Title Description

Once upon a kingdom, in n between cities has m path is connected, and at most any road directly connected between the two cities. After a serious war, there are d roads have been destroyed. The king wants to repair the road system in the country, there are now two major cities A and B traffic between the interrupt, the king hoped to restore the connection between the two cities as soon as possible. Your task is to repair some of the roads A and B minimum connection between the restoration and repair of road length requirements.

Input Format

A first behavior file input integer n- ( 2 <n≤100 ), represents the number of cities. These cities are numbered from 1 to the n- .

Conduct a second integer m ( n--1≤m≤2n (n--. 1) ), the number of road representation.

The next m lines, each line . 3 integers I, J, K ( 1≤i, j ≦ N, I ≠ J, 0 <k≤100 ), represents city II I and jj have a length between J K connected to the road.

Next, a behavior of an integer d ( 1≤d≤m ), representing the number of post-war damaged roads. In the next d rows, each row two integers i and j , represent city II i and jj road directly connected between j is destroyed.

The last row of two integers A and B, on behalf of two major cities need to restore traffic.

Output Format

The output file a single integer representing restore A and the minimum total length of road traffic between A and B in need of repair.

Destroyed some paths, fix his price is the length of the path, not the path of destruction, fix it at the cost of 0 (* Front chain to build the star map)

SPFA for the most short-circuit:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <queue>
 6 using namespace std;
 7 int n,m,d;
 8 int a,b;
 9 int x,y;
10 int dis[10000];
11 bool vis[10000];
12 bool mark[1000][1000];
13 struct node
14 {
15     int x,y,w;
16 }edge[10000];
17 struct ask
18 {
19     int w;
20     int to;
21     int next;
22 }ed[10000];
23 int head [10000];
24 int tot =0;
25 void add (int u,int v,int w)
26     {
27       ed[++tot].w=w;
28       ed[tot].to=V;
 29        ED [TOT] = .next head [U];
 30        head [U] = TOT;
 31 is  }
 32  void the SPFA ( int S)
 33 is  {
 34 is        Queue < int > Q;
 35          for ( int I = . 1 ; I <= n-; I ++) { // initialization, 
36              DIS [I] = 0x7FFFFFFF ;
 37 [              VIS [I] = 0 ;
 38 is          }
 39          q.push (S); // put starting 
40          DIS [S] =0 ; // themselves to themselves as 0; 
41 is          VIS [S] = . 1 ;
 42 is          the while (! Q.empty ()) { 
 43 is              int U = q.front ();
 44 is              q.pop (); VIS [U] = 0 ; // pop-up head of the queue 
45              for ( int I = head [U]; I; I = ED [I] .next) {
 46 is                  int V = ED [I] .to;
 47                  IF (VIS [V]) Continue ;
 48                  IF (DIS [V]> DIS [U] + ED [I] .W) {
 49                      DIS [V] = DIS [U] + ED [I] .W;
 50                     if(!vis[v]){
51                         vis[v]=1;
52                         q.push(v);//重新放入; 
53                     }
54                 }
55             }
56         }
57 }
58 int main()
59 {
60     scanf ("%d",&n);
61     scanf ("%d",&m);
62     for (int i = 1;i <= m;i++)
63     {
64         scanf ("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].w);
65     }
66     scanf ("%d",&d);
67     for (int i = 1;i <= d;i++)
68     {
69         scanf ("%d%d",&x,&y);
70         mark[x][y]=1;
71         mark[y][x]=1;
72     }
73     scanf ("%d%d",&a,&b);
74     for (int i = 1;i <= m;i++)
75     {
76         if (mark[edge[i].x][edge[i].y]==1)
77         {
78             add(edge[i].x,edge[i].y,edge[i].w);    
79             add(edge[i].y,edge[i].x,edge[i].w);
80         }
81         else 
82         {
83             add(edge[i].x,edge[i].y,0);
84             add(edge[i].y,edge[i].x,0);     
85         }
86     }
87     SPFA(a);
88     cout<<dis[b]<<endl;
89     return 0;
90 }

 

Guess you like

Origin www.cnblogs.com/very-beginning/p/12294522.html