• PAT (Advanced level) 1003. Emergency (25) Dijkstra


    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

    题意:
    求从C1 到 C2的最短路方案数最大权值
    题解:dijkstra
    当距离相同时记得叠加方案数。
      1 #include <stdio.h>
      2 #include <iostream>
      3 #include <vector>
      4 #include <queue>
      5 #include <algorithm>
      6 #include <utility>
      7 #include <string.h>
      8 #define MAXX 100010
      9 #define MMF(x) memset(x, 0, sizeof(x))
     10 #define MMI(x) memset(x, INF, sizeof(x))
     11 using namespace std;
     12 
     13 const int INF = 0x3f3f3f3f;
     14 const int N = 600;
     15 
     16 struct sion
     17 {
     18     int dic;
     19     int amt;
     20     int pcnt;
     21 }C[N];
     22 int mp[N][N];
     23 bool vis[N];
     24 int team[N];
     25 
     26 void init(int &n)
     27 {
     28     MMF(vis);
     29     for(int i = 0; i < n; i++)
     30     {
     31         C[i].dic = INF;
     32         C[i].pcnt = 1;
     33         C[i].amt = 0;
     34         for(int j = 0; j < n; j++)
     35             mp[i][j] = INF;
     36     }
     37 
     38 }
     39 void dijkstra(int &s, int &n)
     40 {
     41     queue<int>q;
     42     vis[s] = 1;
     43     C[s].dic = 0;
     44     C[s].amt = team[s];
     45     q.push(s);
     46     //
     47     while(!q.empty())
     48     {
     49         //cout << "~";
     50         int now = q.front();
     51         q.pop();
     52         for(int i = 0; i < n; i++)
     53         {
     54             if(!vis[i])
     55             {
     56 
     57                 if(C[i].dic > C[now].dic + mp[now][i])
     58                 {
     59                     C[i].dic = C[now].dic + mp[now][i];
     60                     C[i].amt = C[now].amt + team[i];
     61                     C[i].pcnt = C[now].pcnt;
     62                 }
     63                 else if(C[i].dic == C[now].dic + mp[now][i])
     64                 {
     65                     C[i].pcnt += C[now].pcnt;
     66                     if(C[i].amt < C[now].amt + team[i])
     67                         C[i].amt = C[now].amt + team[i];
     68                 }
     69             }
     70         }
     71         int mi = INF;
     72         int x;
     73         for(int i = 0; i < n; i++)
     74         {
     75             if(!vis[i] && C[i].dic < mi)
     76             {
     77                 mi = C[i].dic;
     78                 x = i;
     79             }
     80             //cout << C[i].dic << endl;
     81         }
     82         if(mi == INF)
     83             break;
     84         q.push(x);
     85         vis[x] = 1;
     86     }
     87     return ;
     88 }
     89 
     90 int main()
     91 {
     92     int n, m, s, t;
     93     int x, y, z;
     94     scanf("%d%d%d%d", &n, &m, &s, &t);
     95     for(int i = 0; i < n; i++)
     96         scanf("%d", &team[i]);
     97     init(n);
     98     for(int i = 0; i < m; i++)
     99     {
    100         scanf("%d%d%d", &x, &y, &z);
    101         if(mp[x][y] >= z)
    102             mp[x][y] = mp[y][x] = z;
    103     }
    104     dijkstra(s, n);
    105 
    106     printf("%d %d
    ", C[t].pcnt, C[t].amt);
    107 
    108 }
    
    
    
     
  • 相关阅读:
    H公司以及我的目标
    新的起点
    apache服务器配置Net的实践
    会计简要学习
    二、MongoDB的简单增删改查
    一、MongoDB安装与启动
    KnockOutJs初次体验
    DevExpress 全体窗口换肤的功能 winform
    DevExporess 右键菜单的实现
    使用gridControl gridview总结
  • 原文地址:https://www.cnblogs.com/Yumesenya/p/5708357.html
Copyright © 2020-2023  润新知