zoukankan      html  css  js  c++  java
  • PAT.Emergency(求最短路条数 + 最短路中点权和最大的那条最短路的点权和)

    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 Specification:

    Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤) - the number of cities (and the cities are numbered from 0 to N1), 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 Specification:

    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


    /*
        本题大意:无向图,求结点之间的最短路的条数,并求出所有最短路中点权和最大的那条最短路的点权和。
       本题思路:其实本题正规解法是spfa + 最大流,但是长时间不写网络流有点生疏,加上PAT甲级不考网络流哈哈哈,
      就写了个dfs版本的。
    */ #include <iostream> #include <cstring> #include <queue> #include <algorithm> using namespace std; const int maxn = 500 + 5, inf = 0x3f3f3f3f; struct edge { int to, w, _next; } edges[maxn * maxn]; int n, m, c1, c2, u, v, w; int head[maxn]; int cnt; void init() { memset(head, -1, sizeof head); cnt = 0; } void addedge(int u, int v, int w) { edges[cnt].to = v; edges[cnt].w = w; edges[cnt]._next = head[u]; head[u] = cnt ++; } int dist[maxn]; bool vis[maxn]; void spfa(int s, int t) { memset(dist, inf, sizeof dist); memset(vis, false, sizeof vis); vis[s] = true; dist[s] = 0; queue<int> que; que.push(s); while(!que.empty()) { int u = que.front(); que.pop(); vis[u] = false; for(int k = head[u]; ~k; k = edges[k]._next) { int v = edges[k].to; if(dist[v] > dist[u] + edges[k].w) { dist[v] = dist[u] + edges[k].w; if(!vis[v]) { vis[v] = true; que.push(v); } } } } } int rescues[maxn]; int num, ans, temp; void dfs(int u, int pre) { if(u == c2) { ans = max(ans, temp); num ++; return; }; for(int k = head[u]; ~k; k = edges[k]._next) { int v = edges[k].to; if(edges[k].w + dist[u] == dist[v]) { temp += rescues[v]; dfs(v, u); temp -= rescues[v]; } } } int main() { init(); cin >> n >> m >> c1 >> c2; for(int i = 0; i < n; i ++) cin >> rescues[i]; while(m --) { cin >> u >> v >> w; addedge(u, v, w); addedge(v, u, w); } spfa(c1, c2); dfs(c1, c2); cout << num << ' ' << ans + rescues[c1] << endl; return 0; }
     
  • 相关阅读:
    一些javascript的常用方法(转载)
    php在线处理视频 (在线视频转码,取得视频信息)
    在JS中最常看到切最容易迷惑的语法(转)
    jquery ui resizable bug
    设计技巧
    超大记录量数据库设计
    相似度分析(转)
    linux vi 替换命令
    今天发现一个关于CSS的东东,解决了我很多疑问
    Http Handler 介绍
  • 原文地址:https://www.cnblogs.com/bianjunting/p/12491403.html
Copyright © 2011-2022 走看看