zoukankan      html  css  js  c++  java
  • PAT1003:Emergency

    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

    思路

    PAT1030 一样的思路,Dijkstra + DFS的结合求解就行,只不过这道题只要求输出对应最短路径的条数和征召的最多的小组数,比1030要简单点。

    代码
    #include<vector>
    #include<iostream>
    #include<math.h>
    using namespace std;
    const int Inf = pow(2,30);
    vector<int> team(502);
    vector<vector<int>> graph(502,vector<int>(502,Inf));
    vector<int> dis(502,Inf);
    vector<vector<int>> pre(502);
    vector<bool> visit(502,false);
    int n,m,c1,c2;
    vector<vector<int>> paths;
    vector<int> tmp;
    int maxteamnum = -1;
    
    void dfs(int v)
    {
        if(v == c1)
        {
          int tmpteamnum = 0;
          tmp.push_back(v);
          paths.push_back(tmp);
          for(int i = tmp.size() - 1;i >= 0;i--)
          {
              tmpteamnum += team[tmp[i]];
          }
          if(maxteamnum < tmpteamnum)
          {
              maxteamnum = tmpteamnum;
          }
          tmp.pop_back();
          return;
        }
        tmp.push_back(v);
        for(int i = 0;i < pre[v].size();i++)
            dfs(pre[v][i]);
        tmp.pop_back();
    }
    
    int main()
    {
      cin >> n >> m >> c1 >> c2;
      for(int i = 0;i < n;i++)
      {
          cin >> team[i];
      }
      for(int i = 0;i < m;i++)
      {
          int s,d;
          cin >> s >> d;
          cin >> graph[s][d];
          graph[d][s] = graph[s][d];
      }
    
      pre[c1].push_back(-1);
      dis[c1] = 0;
    
      //Dijkstra
      for(int i = 0;i < n;i++)
      {
          int u = -1,mindis = Inf;
          for(int j = 0;j < n;j++)
          {
              if(!visit[j] && dis[j] < mindis)
              {
                  u = j;
                  mindis = dis[j];
              }
          }
          if( u == -1)
            break;
          visit[u] = true;
          for(int v = 0;v < n;v++)
          {
             if(!visit[v] && graph[u][v] != Inf)
             {
                 if(dis[u] + graph[u][v] < dis[v])
                 {
                     dis[v] = dis[u] + graph[u][v];
                     pre[v].clear();
                     pre[v].push_back(u);
                 }
                 else if(dis[u] + graph[u][v] == dis[v])
                     pre[v].push_back(u);
             }
          }
      }
      dfs(c2);
      cout << paths.size() << " " << maxteamnum << endl;
    }
     
  • 相关阅读:
    webpack入门
    Javascript隐式转换
    一个最小手势库的实现
    运用JS设置cookie、读取cookie、删除cookie
    不同浏览器下兼容文本两端对齐
    使用CSS3实现一个3D相册
    JavaScript 火的有点过头了,但又能火多久呢?
    强大的css3
    CSS3与页面布局学习总结
    红米手机真机调试问题记录
  • 原文地址:https://www.cnblogs.com/0kk470/p/7743834.html
Copyright © 2011-2022 走看看