zoukankan      html  css  js  c++  java
  • A1003. Emergency (25)

    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.

    Sample Input

    5 6 0 2
    1 2 1 5 3
    0 1 1
    0 2 2
    0 3 1
    1 2 1
    2 4 1
    3 4 1
    

    Sample Output

    2 4
    
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <iostream>
     4 #include <string.h>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <string>
     8 #include <stack> 
     9 #include <queue>
    10 #include <vector>
    11 using namespace std;
    12 const int maxn=510;
    13 const int  INF=1000000000;
    14 int G[maxn][maxn];
    15 int d[maxn],w[maxn],num[maxn],weight[maxn];
    16 bool vis[maxn]={false};
    17 int n,m,c1,c2;
    18 void Dij(int s)
    19 {
    20    fill(d,d+maxn,INF);
    21    memset(num,0,sizeof(num));
    22    memset(w,0,sizeof(w));
    23    d[s]=0;
    24    w[s]=weight[s];
    25    num[s]=1;
    26    for(int i=0;i<n;i++)
    27    {
    28        int u=-1,min=INF;
    29        for(int j=0;j<n;j++)
    30        {
    31        if(d[j]<min&&vis[j]==false)
    32        {
    33            u=j;
    34            min=d[j];
    35        }        
    36     }
    37     
    38     if(u==-1)return;
    39     vis[u]=true;
    40     for(int v=0;v<n;v++)
    41     {
    42         if(vis[v]==false&&G[u][v]!=INF)
    43         {
    44             if(d[u]+G[u][v]<d[v])
    45             {
    46                 num[v]=num[u];
    47                 w[v]=w[u]+weight[v];
    48                 d[v]=d[u]+G[u][v];
    49             }else if(d[u]+G[u][v]==d[v])
    50             {
    51                 num[v]+=num[u];
    52                 if(w[v]<w[u]+weight[v])
    53                 {
    54                  w[v]=w[u]+weight[v];
    55                 }
    56                 
    57                 
    58             }
    59             
    60         }
    61     }
    62    }    
    63 }
    64 
    65 int main(){
    66    
    67    scanf("%d%d%d%d",&n,&m,&c1,&c2);
    68    for(int i=0;i<n;i++)
    69    {
    70        scanf("%d",&weight[i]);
    71    } 
    72    fill(G[0],G[0]+maxn*maxn,INF);
    73    for(int i=0;i<m;i++)
    74    {
    75        int u,v;
    76        scanf("%d%d",&u,&v);
    77        scanf("%d",&G[u][v]);
    78        G[v][u]=G[u][v];
    79    }
    80    Dij(c1);
    81    printf("%d %d
    ",num[c2],w[c2]); 
    82     return 0;
    83 }
  • 相关阅读:
    资深工程师为何否定这种单例模式
    C#经典面试题及答案【20090210更新】
    难道SQL的子查询就是鸡肋吗?
    转:WCF基础知识问与答
    针对分析单点登录(流程图与数据安全)提出的问题及解决方案
    老生常谈:装饰者模式
    我对IDisposable接口的理解
    log4net日志组件经验分享
    老生常谈:工厂模式兄弟姐妹
    探讨高访问量网站优化方案(从图片角度)
  • 原文地址:https://www.cnblogs.com/ligen/p/4327900.html
Copyright © 2011-2022 走看看