zoukankan      html  css  js  c++  java
  • PAT 1003 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
    
    作者: CHEN, Yue
    单位: PAT联盟
    时间限制: 400ms
    内存限制: 64MB
    代码长度限制: 16KB

     1 #include<iostream>
     2 #include<vector>
     3 using namespace std;
     4 const int inf = 99999999;
     5 vector<vector<int> > G(505, vector<int>(505, inf));
     6 vector<int> vis(505, false), w(505), dis(505, inf), weight(505, 0), num(505, 0);
     7 int cnt=1;
     8 int main(){
     9   int n, m, s, e, i;
    10   scanf("%d%d%d%d", &n, &m, &s, &e);
    11   for(i=0; i<n; i++) scanf("%d", &w[i]);
    12   for(i=0; i<m; i++){
    13     int start, end, length;
    14     scanf("%d %d %d", &start, &end, &length);
    15     G[start][end] = G[end][start] = length;
    16   }
    17   dis[s] = 0;
    18   weight[s] = w[s];
    19   num[s] = 1;
    20   for(i=0; i<n; i++){
    21     int minn=inf, u=-1;
    22     for(int j=0; j<n; j++){
    23       if(!vis[j] && dis[j]<minn){
    24         minn = dis[j];
    25         u = j;
    26       }
    27     }
    28     vis[u] = true;
    29     if(u==-1) break;
    30     for(int v=0; v<n; v++){
    31       if(!vis[v] && G[u][v] != inf){
    32         if(dis[v] > dis[u]+G[u][v]){
    33           num[v] = num[u];
    34           dis[v] = dis[u] + G[u][v];
    35           weight[v] = weight[u] + w[v];
    36         }else if(dis[v] == dis[u]+G[u][v]){
    37           num[v] += num[u];
    38           if(weight[v] < weight[u]+w[v]) weight[v] = weight[u]+w[v];
    39         }
    40       }
    41     }
    42   }
    43   printf("%d %d", num[e], weight[e]);
    44   return 0;
    45 }

    Bellman法求解

     1 #include<iostream>
     2 #include<vector>
     3 #include<set>
     4 using namespace std;
     5 const int inf = 99999999;
     6 const int MAXV = 510;
     7 struct Node{
     8   int v, dis; //v为邻接边的目标定点, dis为邻接边的边权
     9   Node(int _v, int _dis) : v(_v), dis(_dis){}
    10 };
    11 
    12 vector<Node> Adj[MAXV]; //图的邻接表
    13 //n为定点数, m为边数, st,ed分别为起点和终点, weight[]记录点权
    14 int n, m, st, ed, weight[MAXV];
    15 //d[]记录最短距离, w[]记录最大点权之和, num[]记录最短路径条数
    16 vector<int> d(MAXV, inf), w(MAXV, 0), num(MAXV, 0);
    17 set<int> pre[MAXV]; //前驱, 因为bellman法会重复访问同一个节点,因而用set
    18 
    19 void Bellman(int s){
    20   d[s] = 0;
    21   w[s] = weight[s];
    22   num[s] = 1;
    23   for(int i=0; i<n-1; i++){//执行n-1轮操作
    24     for(int u=0; u<n; u++){//每轮操作访问所有的边
    25       for(int j=0; j<Adj[u].size(); j++){
    26         int v = Adj[u][j].v;
    27         int dis = Adj[u][j].dis;
    28         if(d[u]+dis < d[v]){ //以u为中介的时候能让d[v]变小
    29           d[v] = d[u]+dis;
    30           w[v] = w[u]+weight[v];
    31           num[v] = num[u];
    32           pre[v].clear();
    33           pre[v].insert(u);
    34         }else if(d[u]+dis == d[v]){//找到长度相同的路径
    35           if(w[u]+weight[v] > w[v]){
    36             w[v] = w[u] + weight[v];
    37           }
    38           pre[v].insert(u);
    39           num[v] = 0; //重新统计num[v]
    40           set<int>::iterator it;
    41           for(it = pre[v].begin(); it!=pre[v].end(); it++){
    42             num[v] += num[*it];
    43           }
    44         }
    45       }
    46     }
    47   }
    48 }
    49 
    50 int main(){
    51   scanf("%d%d%d%d", &n, &m, &st, &ed);
    52   for(int i=0; i<n; i++) scanf("%d", &weight[i]);
    53   int u, v, wt;
    54   for(int i=0; i<m; i++){
    55     scanf("%d%d%d", &u, &v, &wt);
    56     Adj[u].push_back(Node(v, wt));
    57     Adj[v].push_back(Node(u, wt));
    58   }
    59   Bellman(st);
    60   printf("%d %d
    ", num[ed], w[ed]);
    61   return 0;
    62 }
  • 相关阅读:
    【9408】数的计数
    【rlz03】十六进制转十进制
    【rlz02】二进制转十进制
    【rlz01】完全数
    【a101】高精度实数加法
    【9406】2的幂次方
    【42.86%】【Codeforces Round #380D】Sea Battle
    【26.83%】【Codeforces Round #380C】Road to Cinema
    【9932】饥饿的牛
    【9933】单词的划分
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9237623.html
Copyright © 2011-2022 走看看