您的位置:首页 > 其它

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

2015-06-06 11:11 369 查看
#include<iostream>
#include<fstream>
using namespace std;
#define INFINITY 21474843649
#define MaxSize 500

int Map[MaxSize][MaxSize];                  // 用邻接矩阵表示带权图
int dist[MaxSize];                                    //  dist[i]存放顶点i的当前最短路径长度
int pathCount[MaxSize];                        //  the number of different shortest paths
int N, M, C1, C2;
int teams[MaxSize];                                //  teams[i] is the number of rescue teams in the i-th cit
bool 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: