• HDU1595


    SPFA

    题意:给定一张图,从1到n。

    求1到n的最短路,但是可能存在某条路被阻断了,即为inf了。

    这条路必定是存在最短路中。所以枚举最短路中的路径。。。。

    View Code
      1 #include<stdio.h>
      2 #include<string.h>
      3 #include<stdlib.h>
      4 #include<queue>
      5 #include<algorithm>
      6 using namespace std;
      7 const int maxn = 1005;
      8 const int maxm = 1005*500;
      9 const int inf = 9999999;
     10 struct node{
     11     int u,val,next;
     12 }edge[ maxm*2 ];
     13 int head[ maxn ],cnt;
     14 int dis[ maxn ],vis[ maxn ],path[ maxn ];
     15 int n,m,CNT;
     16 struct node22{
     17     int x,y;
     18 }road[ maxn ];
     19 int mat[ maxn ][ maxn ];
     20 void init(){
     21     cnt=0;
     22     CNT=0;
     23     memset( path,-1,sizeof(path) );
     24     memset( head,-1,sizeof( head ));
     25 }
     26 void addedge( int a,int b,int c ){
     27     edge[ cnt ].u=b;
     28     edge[ cnt ].val=c;
     29     edge[ cnt ].next=head[ a ];
     30     head[ a ]=cnt++;
     31 }
     32 void changeit(const int &x, const int &y, int va)  
     33 {  
     34     for( int i=head[x];i!=-1;i=edge[i].next ){
     35         if( edge[i].u==y ){
     36             edge[i].val=va;
     37         }
     38     }
     39 }  
     40 
     41 void spfa( int s ){
     42     for( int i=1;i<=n;i++ ){
     43         dis[i]=inf;
     44         vis[i]=0;
     45         //path[i]=s;
     46     }
     47     queue<int>q;
     48     while( !q.empty() )
     49         q.pop();
     50     vis[s]=1;
     51     dis[s]=0;
     52     q.push( 1 );
     53     while( !q.empty() ){
     54         int now=q.front();
     55         q.pop();
     56         vis[ now ]=0;
     57         for( int i=head[ now ];i!=-1;i=edge[i].next ){
     58             int next=edge[i].u;
     59             if( dis[next]>dis[now]+edge[i].val ){
     60                 dis[next]=dis[now]+edge[i].val;
     61                 path[next]=now;
     62                 if( vis[next]==0 ){
     63                     vis[next]=1;
     64                     q.push(next);
     65                 }
     66             }
     67         }
     68     }
     69     return ;
     70 }
     71 
     72 int main(){
     73     while( scanf("%d%d",&n,&m)!=EOF ){
     74         int a,b,c;
     75         init();
     76         while( m-- ){
     77             scanf("%d%d%d",&a,&b,&c);
     78             addedge( a,b,c );
     79             addedge( b,a,c );
     80             mat[a][b]=mat[b][a]=c;
     81         }
     82         spfa( 1 );
     83         for( int i=n;i!=1;i=path[i] ){
     84             road[ CNT ].x=i;
     85             road[ CNT ].y=path[i];
     86             CNT++;
     87         }
     88         int ans=0;
     89         for(int i = 0; i < CNT; i++){  
     90             changeit(road[i].x, road[i].y, inf);  
     91             changeit(road[i].y, road[i].x, inf);   
     92             spfa(1);  
     93             if(dis[n] > ans&&dis[n]!=inf) ans = dis[n];  
     94             changeit(road[i].x, road[i].y, mat[road[i].x][road[i].y]);  
     95             changeit(road[i].y, road[i].x, mat[road[i].x][road[i].y]);  
     96         }  
     97         printf("%d\n", ans);
     98     }
     99     return 0;
    100 }
    keep moving...
  • 相关阅读:
    RFS入门【JS脚本应用】
    robot framework安装问题排查
    Jmeter报告模板(3种)
    【Ant】问题汇总
    上网痕迹查询助手Viewurl 2017
    Sublime3使用问题汇总
    Git常用命令
    cmp快排 结构体快排
    GCD LCM 最大公约数 最小公倍数 分数模板 (防溢出优化完成)
    三分法 模板
  • 原文地址:https://www.cnblogs.com/xxx0624/p/2921427.html
Copyright © 2020-2023  润新知