JZOJ 1212. 重建道路

题目

Description

          Jby在玩一个“辉煌帝国”的网络游戏…
          他的帝国有N个城市,城市之前总共有M条道路,每条道路连接两个不同的城市。Jby给出了每条道路的长度。
          最近他由于得罪了别的玩家,结果被人家炮轰了一回,好在Jby实力强大,城市都没有被破坏,不过有其中D条道路被破坏了。这将可能导致两个军事重镇A和B之间的通讯,Jby找来了你,要你修复一些被破坏的道路,使得A和B能够重新建立通讯起来,而且由于Jby在这场战争已经用了很多钱了,所以他要求被修复的道路的总长度要最小
 

Input

          第一行有一个整数N(2<=N<=100),表示有多少个城市。城市的编号是1~N。第二行有一个整数M(N-1<=M<=N*(N-1)/2,表示有多少条道路。接下来有M行,每行3个整数, x,y,len(1<=x,y<=n,x<>y. 0< len <100)表示城市x和城市y有一条长度为len的双向道路。再接下来的一行有一个整数D(1<=D<=M),表示有D条道路破坏了,然后下面有D行,每行两个整数x,y表示x和y之间的道路被破坏了。 最后一行是两个整数A,B,表示两个重要城市A和B的编号。

Output

仅有一行,代表修复道路的最小费用。
 

Sample Input

3
2
1 2 1
2 3 2
1
1 2
1 3

Sample Output

1
 

Data Constraint

 

分析

  • 水,直接spfa

 

代码

 1 #include<iostream>
 2 #include<queue>
 3 #include<vector>
 4 #include<cstring>
 5 #include<cstdio>
 6 using namespace std;
 7 vector<int> f[1001];
 8 int a[201][201],map[201][201],dis[201],vis[201];
 9 void spfa(int s)
10 {
11     memset(dis,0x3f,sizeof(dis));
12     queue<int> q;
13     q.push(s); vis[s]=1; dis[s]=0;
14     while (!q.empty())
15     {
16         int x=q.front(); q.pop(); vis[x]=0;
17         for (int i=0;i<f[x].size();i++)
18         {
19             int y=f[x][i];
20             if (dis[x]+map[x][y]<dis[y])
21             {
22                 dis[y]=dis[x]+map[x][y];
23                 if (!vis[y])
24                 {
25                     vis[y]=1;
26                     q.push(y);
27                 }
28             }
29         }
30     }
31 }
32 int main ()
33 {
34     freopen("road.in","r",stdin);
35     freopen("road.out","w",stdout);
36     int n,m; scanf("%d%d",&n,&m);
37     for (int i=1,x,y,z;i<=m;i++)
38     {
39         scanf("%d%d%d",&x,&y,&z);
40         f[x].push_back(y);
41         f[y].push_back(x);
42         a[x][y]=z;
43         a[y][x]=z;
44     }
45     int D; scanf("%d",&D);
46     for (int i=1,x,y;i<=D;i++)
47     {
48          scanf("%d%d",&x,&y);
49          map[y][x]=map[x][y]=a[x][y];
50     }
51     int s,t;
52     scanf("%d%d",&s,&t);
53     spfa(s);
54     cout<<dis[t];
55  } 
为何要逼自己长大,去闯不该闯的荒唐
原文地址:https://www.cnblogs.com/zjzjzj/p/11187427.html