洛谷P1186 玛丽卡 spfa+删边

洛谷P1186 玛丽卡
http://blog.csdn.net/huihao123456/article/details/73414139

题目描述

麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复。

因为她和他们不住在同一个城市,因此她开始准备她的长途旅行。

在这个国家中每两个城市之间最多只有一条路相通,并且我们知道从一个城市到另一个城市路上所需花费的时间。

麦克在车中无意中听到有一条路正在维修,并且那儿正堵车,但没听清楚到底是哪一条路。无论哪一条路正在维修,从玛丽卡所在的城市都能到达麦克所在的城市。

玛丽卡将只从不堵车的路上通过,并且她将按最短路线行车。麦克希望知道在最糟糕的情况下玛丽卡到达他所在的城市需要多长时间,这样他就能保证他的女朋友离开该城市足够远。

编写程序,帮助麦克找出玛丽卡按最短路线通过不堵车道路到达他所在城市所需的最长时间(用分钟表示)。

输入输出格式

输入格式:

第一行有两个用空格隔开的数N和M,分别表示城市的数量以及城市间道路的数量。1≤N≤1000,1≤M≤N*(N-1)/2。城市用数字1至N标识,麦克在城市1中,玛丽卡在城市N中。

接下来的M行中每行包含三个用空格隔开的数A,B和V。其中1≤A,B≤N,1≤V≤1000。这些数字表示在A和城市B中间有一条双行道,并且在V分钟内是就能通过。

输出格式:

输出文件的第一行中写出用分钟表示的最长时间,在这段时间中,无论哪条路在堵车,玛丽卡应该能够到达麦克处,如果少于这个时间的话,则必定存在一条路,该条路一旦堵车,玛丽卡就不能够赶到麦克处。

输入样例#1:
5 7
1 2 8
1 4 10
2 3 9
2 4 10
2 5 1
3 4 7
3 5 10
输出样例#1:
27



  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 using namespace std;
  6 const int maxn=1000+5;
  7 const int maxm=1000*1000;
  8 int read()
  9 {
 10     int x=0,f=1;
 11     char ch;
 12     ch=getchar();
 13     while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
 14     while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
 15     return x*f;
 16 }
 17 int n,m,num=0;
 18 int head[maxn],d[maxn],inq[maxn],g[maxn][maxn],team[maxm],pre[maxn];
 19 struct edges
 20 {
 21     int next,to,dist;
 22 } e[maxm];
 23 void add(int from,int to,int dist)
 24 {
 25     e[++num].next=head[from];
 26     e[num].to=to;
 27     e[num].dist=dist;
 28     head[from]=num;
 29 }
 30 void spfa()
 31 {
 32     memset(inq,0,sizeof(inq));
 33     memset(team,0,sizeof(team));
 34     memset(d,0x7f,sizeof(d));
 35     int h=0,t=1;
 36     team[1]=1;
 37     inq[1]=1;
 38     d[1]=0;
 39     do
 40     {
 41         h++;
 42         int u=team[h];
 43         inq[u]=0;
 44         for(int i=head[u];i;i=e[i].next)
 45         {
 46             int to=e[i].to;
 47             if(d[to]>d[u]+e[i].dist)
 48             {
 49                 d[to]=d[u]+e[i].dist;
 50                 pre[to]=u;//记录前驱
 51                 if(!inq[to])
 52                 {
 53                     inq[to]=1;
 54                     team[++t]=to;
 55                 }
 56             }
 57         }
 58     }while(h!=t);
 59 }
 60 void spfa1()
 61 {
 62     memset(inq,0,sizeof(inq));
 63     memset(team,0,sizeof(team));
 64     memset(d,0x7f,sizeof(d));
 65     int h=0,t=1;
 66     team[1]=1;
 67     inq[1]=1;
 68     d[1]=0;
 69     while(h<t)
 70     {
 71         h++;
 72         int u=team[h];
 73         inq[u]=0;
 74         for(int i=head[u];i;i=e[i].next)
 75         {
 76             int to=e[i].to;
 77             if(!g[u][to])//判断边是否已删
 78             {
 79             if(d[to]>d[u]+e[i].dist)
 80             {
 81                 d[to]=d[u]+e[i].dist;
 82                 if(!inq[to])
 83                 {
 84                     inq[to]=1;
 85                     team[++t]=to;
 86                 }
 87             }
 88             }
 89         }
 90     }
 91 }    
 92 int main()
 93 {
 94     int i;
 95     n=read();m=read();
 96     int x,y,z;
 97     for(i=1;i<=m;i++)
 98     {  scanf("%d%d%d",&x,&y,&z);
 99         add(x,y,z);add(y,x,z);
100     }
101     spfa();
102     int maxx=0;
103     int k=n;
104    while(k>1)//删边(轮流删一遍) 
105     {
106         g[k][pre[k]]=g[pre[k]][k]=1;
107         spfa1();
108         g[k][pre[k]]=g[pre[k]][k]=0;//恢复被删的边,转删其他的边
109         k=pre[k];//递归记录前驱
110         maxx=max(maxx,d[n]);
111     }
112     printf("%d",maxx);
113 }
    
欢迎大家吐槽或评论,如要转载请注明地址,谢谢~(虽然不一定有人能看到。。。)
原文地址:https://www.cnblogs.com/Slager-Z/p/7441767.html