pat 团体天梯 L3-011. 直捣黄龙

L3-011. 直捣黄龙

时间限制
150 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

本题是一部战争大片 —— 你需要从己方大本营出发,一路攻城略地杀到敌方大本营。首先时间就是生命,所以你必须选择合适的路径,以最快的速度占领敌方大本营。当这样的路径不唯一时,要求选择可以沿途解放最多城镇的路径。若这样的路径也不唯一,则选择可以有效杀伤最多敌军的路径。

输入格式:

输入第一行给出2个正整数N(2 <= N <= 200,城镇总数)和K(城镇间道路条数),以及己方大本营和敌方大本营的代号。随后N-1行,每行给出除了己方大本营外的一个城镇的代号和驻守的敌军数量,其间以空格分隔。再后面有K行,每行按格式“城镇1 城镇2 距离”给出两个城镇之间道路的长度。这里设每个城镇(包括双方大本营)的代号是由3个大写英文字母组成的字符串。

输出格式:

按照题目要求找到最合适的进攻路径(题目保证速度最快、解放最多、杀伤最强的路径是唯一的),并在第一行按照格式“己方大本营->城镇1->...->敌方大本营”输出。第二行顺序输出最快进攻路径的条数、最短进攻距离、歼敌总数,其间以1个空格分隔,行首尾不得有多余空格。

输入样例:
10 12 PAT DBY
DBY 100
PTA 20
PDS 90
PMS 40
TAP 50
ATP 200
LNN 80
LAO 30
LON 70
PAT PTA 10
PAT PMS 10
PAT ATP 20
PAT LNN 10
LNN LAO 10
LAO LON 10
LON DBY 10
PMS TAP 10
TAP DBY 10
DBY PDS 10
PDS PTA 10
DBY ATP 10
输出样例:
PAT->PTA->PDS->DBY
3 30 210

思路:最短路+dfs.要注意要输出的第二行第一个数据是路径的条数,就是最短路径的条数,不是指最优路径有多少个节点,理解错了。。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<set>
#include<queue>
using namespace std;
#define N_MAX 200+5
#define INF 0x3f3f3f3f
int n,k;
string s, t;
string town[N_MAX];
int Num[N_MAX];
map<string, int>M;
int road[N_MAX];
struct edge {
  int to, cost;
  edge() {}
  edge(int to,int cost):to(to),cost(cost) {}
};
struct P{
  int first, second;//first是距离,second是编号
  P() {}
  P(int first,int second):first(first),second(second) {}
  bool operator < (const P&b) const{
    return first > b.first;
  }
};
vector<edge>G[N_MAX];
int d[N_MAX];
vector<int>Prev[N_MAX];

void init() {
  for (int i = 0; i < n; i++) { G[i].clear(); Prev[i].clear(); }
}

void dijkstra(int s) {
  priority_queue<P>que;
  fill(d, d + n, INF);
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top(); que.pop();
    if (p.first > d[p.second])continue;
    int v = p.second;
    for (int i = 0; i < G[v].size();i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        Prev[e.to].clear();
        Prev[e.to].push_back(v);
        que.push(P(d[e.to], e.to));
      }
      else if (d[e.to] == d[v] + e.cost) {
        Prev[e.to].push_back(v);
      }
    }
  }
}

int max_cnt = -1, max_num = -1;//!!
int num_road;//路径条数
vector<int>r;
void dfs(int x,int cnt,int num) {
  road[cnt] = x;
  if (x== 0) {//到达起点
    num_road++;
    if (max_cnt < cnt) {
      max_cnt = cnt;
      max_num = num;//!!!!
      r.clear();
      for (int i = cnt; i >= 0; i--)r.push_back(road[i]);
    }
    else if (max_cnt == cnt&&max_num < num) {
      max_num = num;
      r.clear();
      for (int i = cnt; i >= 0; i--)r.push_back(road[i]);
    }
    return;
  }
    for (int i = 0; i < Prev[x].size();i++) {
      int v = Prev[x][i];
      dfs(v, cnt + 1, num + Num[v]);
   }
}

int main() {
  while (cin>>n>>k>>s>>t) {
    init();
     M[s] = 0; town[0] = s; Num[0] = 0;
    for (int i = 1; i < n;i++) {
      cin >> town[i]; scanf("%d",&Num[i]);
      M[town[i]] = i;
    }
    for (int i = 0; i < k;i++) {
      string from, to; int dist;
      cin >> from >> to; scanf("%d",&dist);
      int F = M[from], T = M[to];
      G[F].push_back(edge(T, dist));
      G[T].push_back(edge(F, dist));
      
    }
    dijkstra(0);
    max_cnt = -1, max_num = -1;//!!!
    num_road = 0;//!!!
    dfs(M[t], 0, Num[M[t]]);//!!
    for (int i = 0; i < r.size(); i++) { 
      cout << town[r[i]];
      if (i + 1 == r.size())puts("");
      else printf("->");
    }
    printf("%d %d %d
",num_road,d[M[t]],max_num);
  }
  return 0;
}
原文地址:https://www.cnblogs.com/ZefengYao/p/8504184.html