码迷,mamicode.com
首页 > 其他好文 > 详细

1003. Emergency

时间:2015-02-27 13:42:44      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:c++   pat   

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std;

#define MAX 501
void Dijkistra(int st);

const int INF=1000000000;
//n-citynum,m-roadnum,st-start,ed-end
//G weight
//d[]the shortest road,w[]the biggest weight,num[]the num of d[]
int n,m,st,ed,G[MAX][MAX],weight[MAX];
int d[MAX],w[MAX],num[MAX];
bool visit[MAX]={false};

int main ()
{
    scanf("%d %d %d %d",&n,&m,&st,&ed);
    int i;
    for( i=0;i<n;i++)
    {
         scanf("%d",weight+i);
         }
    int u,v;
    fill(G[0],G[0]+MAX*MAX,INF); //initial G
    for( i=0;i<m;i++)
    {
         scanf("%d %d",&u,&v);
         scanf("%d",&G[u][v]);
         G[v][u]=G[u][v];//no direction road
         }
    Dijkistra(st);
    printf("%d %d\n",num[ed],w[ed]);
    system("pause");
    return 0;
    } 

void Dijkistra(int s)
{
     fill(d,d+MAX,INF);//initial d
     memset(num,0,sizeof(num));//initial num
     memset(w,0,sizeof(w));//initial w
     d[s]=0;
     w[s]=weight[s];
     num[s]=1;
     for( int i=0;i<n;i++)
     {
          int u=-1,MIN=INF;
          for( int j=0;j<n;j++)
          {
               if( visit[j]==false && d[j]<MIN)
               {
                   u=j;
                   MIN=d[j];
                   }
               }
          if(u==-1) return ;
          visit[u]=true;
          //renew the road
          for( int v=0;v<n;v++)
          {
               if( visit[v]==false && G[u][v]!=INF)
               {
                   if( d[u]+G[u][v]<d[v])
                   {
                       d[v]=d[u]+G[u][v];
                       w[v]=w[u]+weight[v];
                       num[v]=num[u];
                       }
                   else if(d[u]+G[u][v]==d[v])//the same
                   {
                        if(w[u]+weight[v]>w[v])
                           w[v]=w[u]+weight[v];
                        num[v]+=num[u];
                        }
                   }
               }//for
          }//for
     }

1003. Emergency

标签:c++   pat   

原文地址:http://blog.csdn.net/lchinam/article/details/43965997

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!