zoukankan      html  css  js  c++  java
  • L2-001 紧急救援 (dijkstra+dfs回溯路径)

    L2-001 紧急救援 (25 分)

    作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上。当其他城市有紧急求助电话给你的时候,你的任务是带领你的救援队尽快赶往事发地,同时,一路上召集尽可能多的救援队。

    输入格式:

    输入第一行给出4个正整数N、M、S、D,其中N(2N500)是城市的个数,顺便假设城市的编号为0 ~ (N1);M是快速道路的条数;S是出发地的城市编号;D是目的地的城市编号。

    第二行给出N个正整数,其中第i个数是第i个城市的救援队的数目,数字间以空格分隔。随后的M行中,每行给出一条快速道路的信息,分别是:城市1、城市2、快速道路的长度,中间用空格分开,数字均为整数且不超过500。输入保证救援可行且最优解唯一。

    输出格式:

    第一行输出最短路径的条数和能够召集的最多的救援队数量。第二行输出从S到D的路径中经过的城市编号。数字间以空格分隔,输出结尾不能有多余空格。

    输入样例:

    4 5 0 3
    20 30 40 10
    0 1 1
    1 3 2
    0 3 3
    0 2 2
    2 3 2
    

    输出样例:

    2 60
    0 1 3
    这个题和http://www.cnblogs.com/1013star/p/10357601.html如出一辙
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<vector>
      5 #include<queue>
      6 #include<stack>
      7 #include<algorithm>
      8 using namespace std;
      9 const int inf=0x3f3f3f3f;
     10 const int maxn=510;
     11 int n,m,s,d;
     12 int cnt=0,sum=0,res=0;
     13 struct node{
     14     int pos;
     15     int len;
     16     node(){}
     17     node(int pos,int len):pos(pos),len(len){}
     18     friend bool operator < (node a,node b)
     19     {
     20         return a.len>b.len;
     21     }
     22 }head,tail;
     23 vector<node>v[maxn];
     24 vector<int>pre[maxn];
     25 vector<int>tpath;
     26 vector<int>path;
     27 int vis[maxn],dis[maxn];
     28 int p[maxn];
     29 void dijkstra(int st)
     30 {
     31     priority_queue<node>q;
     32     dis[st]=0;
     33     head.pos=st;
     34     head.pos=0;
     35     q.push(head);
     36     while(!q.empty())
     37     {
     38         head=q.top();
     39         q.pop();
     40         if(vis[head.pos])
     41             continue;
     42         vis[head.pos]=1;
     43         int now=head.pos;
     44         for(int i=0;i<v[now].size();i++)
     45         {
     46             tail=v[now][i];
     47             int to=tail.pos;
     48             int len=tail.len;
     49             if(dis[to]>dis[head.pos]+len)
     50             {
     51                 dis[to]=dis[head.pos]+len;
     52                 pre[to].clear();
     53                 pre[to].push_back(head.pos);
     54                 q.push(tail);
     55             }
     56             else if(dis[to]==dis[head.pos]+len)
     57             {
     58                 pre[to].push_back(head.pos);
     59                 q.push(tail);
     60             }
     61         }
     62     }
     63 }
     64 void dfs(int now)
     65 {
     66     if(now==0)
     67     {
     68         cnt++;
     69         tpath.push_back(now);
     70         sum=0;
     71         for(int i=tpath.size()-1;i>=0;i--)
     72         {
     73             
     74             sum+=p[tpath[i]];
     75         }
     76     //    printf("sum=%d
    ",sum);
     77         if(sum>res)
     78         {
     79             res=sum;
     80             path=tpath;
     81         }
     82         tpath.pop_back();
     83         return;
     84     }
     85     tpath.push_back(now);
     86     for(int i=0;i<pre[now].size();i++)
     87     {
     88         dfs(pre[now][i]);
     89     }
     90     tpath.pop_back();
     91 }
     92 int main()
     93 {
     94     cin>>n>>m>>s>>d;
     95     for(int i=0;i<n;i++)
     96     {
     97         scanf("%d",&p[i]);
     98     }
     99     memset(dis,inf,sizeof(dis));
    100     int x,y,w;
    101     for(int i=0;i<m;i++)
    102     {
    103         scanf("%d%d%d",&x,&y,&w);
    104     
    105         node tmp;
    106         tmp.len=w;
    107         tmp.pos=y;
    108         v[x].push_back(tmp);
    109         tmp.pos=x;
    110         v[y].push_back(tmp);
    111     }
    112     dijkstra(s);
    113     dfs(d); 
    114     printf("%d %d
    ",cnt,res);
    115     for(int i=path.size()-1;i>=0;i--)
    116     {
    117         if(i!=0)
    118         printf("%d ",path[i]);
    119         else
    120         printf("%d",path[i]);
    121     }
    122 }
     
  • 相关阅读:
    建筑名称解释
    delphi 文件查找
    bat如何批量删除指定部分文件夹名的文件夹
    在 DELPHI 中 procedure 型变量与 method 型变量的区别
    Spearman Rank(斯皮尔曼等级)相关系数
    机器学习的MLE和MAP:最大似然估计和最大后验估计
    error “Device supports x86, but APK only supports armeabi-v7a”
    windows 安装ninja
    Gradle语法基础解析
    executing external native build for cmake
  • 原文地址:https://www.cnblogs.com/1013star/p/10384568.html
Copyright © 2011-2022 走看看