1003. Emergency (25):简单最短路径问题

来源:互联网 发布:围巾淘宝加盟 编辑:程序博客网 时间:2024/05/19 17:06
#include<iostream>#include<fstream>using namespace std;#define INFINITY 21474843649#define MaxSize 500int Map[MaxSize][MaxSize];                  // 用邻接矩阵表示带权图int dist[MaxSize];                                    //  dist[i]存放顶点i的当前最短路径长度int pathCount[MaxSize];                        //  the number of different shortest pathsint N, M, C1, C2;int teams[MaxSize];                                //  teams[i] is the number of rescue teams in the i-th citbool visited[MaxSize];int amount[MaxSize];                             //  the amount of rescue teams you can possibly gather.void Dijkstra( int Start ){       dist[Start] = 0;       visited[Start]= true;       amount[Start] = teams[Start];       int Vertex = Start;       while( Vertex!=C2 )       {              for( int i=0; i<N; i++ )              {                     if( !visited[i] )                     {                             if( dist[i] > dist[Vertex]+Map[Vertex][i] )                             {                                    dist[i] = dist[Vertex]+Map[Vertex][i];                                    amount[i] = amount[Vertex] + teams[i];                                    pathCount[i] = pathCount[Vertex];                             }                             else                                if( dist[i] == dist[Vertex]+Map[Vertex][i] )       // 当前最短路径 不止一条                                 {                                       pathCount[i] += pathCount[Vertex];                                       if( amount[i] < amount[Vertex] + teams[i] )   // 更新此刻救援队的最大数量                                           amount[i] =  amount[Vertex] + teams[i];                                 }                     }              }              int  minDist = INFINITY;              for( int i=0; i<N; i++  )                if( !visited[i] && dist[i] < minDist )             // 更新 下一个要加入 最短路径中的 顶点                {                       minDist = dist[i];                       Vertex = i;                }             visited[Vertex] = true;       }}int main(){    //ifstream cin("test.txt");    cin>>N>>M>>C1>>C2;    int c1, c2, L;    for( int i=0; i<N; i++ )        cin>>teams[i];    for( int i=0; i<N; i++ )    {         dist[i] = INFINITY;         pathCount[i] = 1;           //at least one path from C1 to C2         visited[i] = false;         for( int j=0; j<N; j++ )            Map[i][j]=INFINITY;    }    for( int i=0; i<M; i++ )    {        cin>>c1>>c2>>L;        Map[c1][c2] = Map[c2][c1] = L;    }    Dijkstra(C1);    cout<<pathCount[C2]<<" "<<amount[C2]<<endl;    return 0;}

0 0
原创粉丝点击