zoukankan      html  css  js  c++  java
  • PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组

    1003. Emergency (25)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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 <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <string>
     5 #include <cstring>
     6 #include <vector>
     7 #include <algorithm>
     8 using namespace std;
     9 
    10 const int maxn = 510;
    11 const int INF = 0x3f3f3f3f;
    12 int n,m,st,ed,G[maxn][maxn],weight[maxn];
    13 int d[maxn],w[maxn],num[maxn];
    14 bool vis[maxn]={false};
    15 
    16 void dij(int s) {
    17     fill(d,d+maxn,INF);
    18     memset(num,0,sizeof(num));
    19     memset(w,0,sizeof(w));
    20     d[s]=0;
    21     w[s]=weight[s];
    22     num[s]=1;
    23     for(int i=0;i<n;++i) {
    24         int u=-1,MIN=INF;
    25         for(int j=0;j<n;++j) {
    26             if(vis[j]==false&&d[j]<MIN) {
    27                 u=j;
    28                 MIN=d[j];
    29             }
    30         }
    31         if(u==-1) return;
    32         vis[u]=true;
    33         for(int v=0;v<n;++v) {
    34             if(vis[v]==false&&G[u][v]!=INF) {
    35                 if(d[u]+G[u][v]<d[v]) {
    36                     d[v]=d[u]+G[u][v];
    37                     w[v]=w[u]+weight[v];
    38                     num[v]=num[u];
    39                 } else if(d[u]+G[u][v]==d[v]) {
    40                     if(w[u]+weight[v]>w[v]) {
    41                         w[v]=w[u]+weight[v];
    42                     }
    43                     num[v]+=num[u];
    44                 }
    45             }
    46         }
    47     }
    48 }
    49 int main() {
    50     scanf("%d%d%d%d",&n,&m,&st,&ed);
    51     for(int i=0;i<n;++i) {
    52         scanf("%d",&weight[i]);
    53     }
    54     int u,v;
    55     fill(G[0],G[0]+maxn*maxn,INF);
    56     for(int i=0;i<m;++i) {
    57         scanf("%d%d",&u,&v);
    58         scanf("%d",&G[u][v]);
    59         G[v][u]=G[u][v];
    60     }
    61     dij(st);
    62     printf("%d %d
    ",num[ed],w[ed]);
    63     return 0;
    64 }
    View Code
  • 相关阅读:
    17 applyMiddleware MainMiddleWare, redux-thunk , createStore
    16 redux简介
    15 react-redux provider组件
    14 React Refs
    13 React 表单与事件
    12 React AJAX
    Vue3 getCurrentInstance与ts结合使用的问题
    Vue3 更改setup中定义的值不渲染到视图上【Vue2.x向Vue3.x的迁移(踩坑)日记】
    Vue3 中组件传值emit【Vue2.x向Vue3.x的迁移日记】
    vue js 模糊匹配搜索查询
  • 原文地址:https://www.cnblogs.com/lemonbiscuit/p/7775967.html
Copyright © 2011-2022 走看看