Source:

PAT A1111 Online Map (30 分)

Description:

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (2), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:

V1 V2 one-way length time

where V1 and V2 are the indices (from 0 to N−1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is the length of the street; and time is the time taken to pass the street.

Finally a pair of source and destination is given.

Output Specification:

For each case, first print the shortest path from the source to the destination with distance D in the format:

Distance = D: source -> v1 -> ... -> destination

Then in the next line print the fastest path with total time T:

Time = T: source -> w1 -> ... -> destination

In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.

In case the shortest and the fastest paths are identical, print them in one line in the format:

Distance = D; Time = T: source -> u1 -> ... -> destination

Sample Input 1:

10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5

Sample Output 1:

Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5

Sample Input 2:

7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5

Sample Output 2:

Distance = 3; Time = 4: 3 -> 2 -> 5

Keys:

Attention:

  • 其实就是求两次最短路径,代码code一遍再copy一遍,考试的时候怎么快怎么来,优化代码反而容易出错

Code:

 /*
Data: 2019-08-18 21:30:18
Problem: PAT_A1111#Online Map
AC: 42:06 题目大意:
给出当前位置和目的地,给出最短路径和最快路径
输入:
第一行给出,结点数N,边数M
接下来M行,v1,v2,单/双向,距离,时间
最后一行,起点,终点
输出:
最短距离,输出路径,不唯一则选择最快的
最短时间,输出路径,不唯一则选择经过结点最少的
*/
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int M=1e3,INF=1e9;
int grap[M][M],cost[M][M],vis[M],d[M],c[M];
int n,m,v1,v2,one,optTime=INF,optLeth=INF;
vector<int> preL[M],preT[M],optL,pathL,optT,pathT; void DijskraL(int s)
{
fill(vis,vis+M,);
fill(d,d+M,INF);
d[s]=;
for(int i=; i<n; i++)
{
int u=-,Min=INF;
for(int j=; j<n; j++)
{
if(vis[j]== && d[j]<Min)
{
Min = d[j];
u = j;
}
}
if(u==-) return;
vis[u]=;
for(int v=; v<n; v++)
{
if(vis[v]== && grap[u][v]!=INF)
{
if(d[u]+grap[u][v] < d[v])
{
preL[v].clear();
preL[v].push_back(u);
d[v] = d[u]+grap[u][v];
}
else if(d[u]+grap[u][v]==d[v])
preL[v].push_back(u);
}
}
}
} void DFSL(int s)
{
if(s == v1)
{
pathL.push_back(v1);
int time=,len=pathL.size();
for(int i=; i<len; i++)
time += cost[pathL[i]][pathL[i-]];
if(time < optTime)
{
optTime = time;
optL = pathL;
}
pathL.pop_back();
return;
}
pathL.push_back(s);
for(int i=; i<preL[s].size(); i++)
DFSL(preL[s][i]);
pathL.pop_back();
} void DijskraT(int s)
{
fill(vis,vis+M,);
fill(c,c+M,INF);
c[s]=;
for(int i=; i<n; i++)
{
int u=-,Min=INF;
for(int j=; j<n; j++)
{
if(vis[j]== && c[j]<Min)
{
Min = c[j];
u = j;
}
}
if(u==-) return;
vis[u]=;
for(int v=; v<n; v++)
{
if(vis[v]== && cost[u][v]!=INF)
{
if(c[u]+cost[u][v] < c[v])
{
preT[v].clear();
preT[v].push_back(u);
c[v] = c[u] + cost[u][v];
}
else if(c[u]+cost[u][v]==c[v])
preT[v].push_back(u);
}
}
}
} void DFST(int s)
{
if(s == v1)
{
pathT.push_back(v1);
if(pathT.size() < optLeth)
{
optLeth = pathT.size();
optT = pathT;
}
pathT.pop_back();
return;
}
pathT.push_back(s);
for(int i=; i<preT[s].size(); i++)
DFST(preT[s][i]);
pathT.pop_back();
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE fill(grap[],grap[]+M*M,INF);
fill(cost[],cost[]+M*M,INF); scanf("%d%d", &n,&m);
for(int i=; i<m; i++)
{
scanf("%d%d%d", &v1,&v2,&one);
scanf("%d%d", &grap[v1][v2],&cost[v1][v2]);
if(!one)
{
grap[v2][v1]=grap[v1][v2];
cost[v2][v1]=cost[v1][v2];
}
}
scanf("%d%d", &v1,&v2);
DijskraL(v1);
DFSL(v2);
DijskraT(v1);
DFST(v2);
if(optT == optL)
{
printf("Distance = %d; Time = %d: %d", d[v2],c[v2],optT[optT.size()-]);
for(int i=optT.size()-; i>=; i--)
printf(" -> %d", optT[i]);
}
else
{
printf("Distance = %d: %d", d[v2], optL[optL.size()-]);
for(int i=optL.size()-; i>=; i--)
printf(" -> %d", optL[i]);
printf("\nTime = %d: %d", c[v2], optT[optT.size()-]);
for(int i=optT.size()-; i>=; i--)
printf(" -> %d", optT[i]);
} return ;
}

PAT_A1111#Online Map的更多相关文章

  1. mapreduce中一个map多个输入路径

    package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...

  2. .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法

    .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...

  3. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  4. Java基础Map接口+Collections

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  5. 多用多学之Java中的Set,List,Map

            很长时间以来一直代码中用的比较多的数据列表主要是List,而且都是ArrayList,感觉有这个玩意就够了.ArrayList是用于实现动态数组的包装工具类,这样写代码的时候就可以拉进 ...

  6. Java版本:识别Json字符串并分隔成Map集合

    前言: 最近又看了点Java的知识,于是想着把CYQ.Data V5迁移到Java版本. 过程发现坑很多,理论上看大部分很相似,实践上代码写起来发现大部分都要重新思考方案. 遇到的C#转Java的一些 ...

  7. MapReduce剖析笔记之八: Map输出数据的处理类MapOutputBuffer分析

    在上一节我们分析了Child子进程启动,处理Map.Reduce任务的主要过程,但对于一些细节没有分析,这一节主要对MapOutputBuffer这个关键类进行分析. MapOutputBuffer顾 ...

  8. MapReduce剖析笔记之七:Child子进程处理Map和Reduce任务的主要流程

    在上一节我们分析了TaskTracker如何对JobTracker分配过来的任务进行初始化,并创建各类JVM启动所需的信息,最终创建JVM的整个过程,本节我们继续来看,JVM启动后,执行的是Child ...

  9. MapReduce剖析笔记之五:Map与Reduce任务分配过程

    在上一节分析了TaskTracker和JobTracker之间通过周期的心跳消息获取任务分配结果的过程.中间留了一个问题,就是任务到底是怎么分配的.任务的分配自然是由JobTracker做出来的,具体 ...

随机推荐

  1. [bzoj3694]最短路_树链剖分_线段树

    最短路 bzoj-3694 题目大意:给你一个n个点m条边的无向图,源点为1,并且以点1为根给出最短路树.求对于2到n的每个点i,求最短路,要求不经过给出的最短路树上的1到i的路径上的最后一条边. 注 ...

  2. backup script

    #!/bin/bash ##################################################### # export the whole database use ex ...

  3. Android图片异步载入框架Android-Universal-Image-Loader

    Android-Universal-Image-Loader是一个图片异步载入,缓存和显示的框架.这个框架已经被非常多开发人员所使用,是最经常使用的几个Android开源项目之中的一个,主流的应用,随 ...

  4. Sahara中的数据模型

    声明: 本博客欢迎转载.但请保留原作者信息,并请注明出处! 作者:郭德清 团队:华为杭州OpenStack团队 本文主要是介绍下Sahara中一些常见的数据模型. 1.Config 用于描写叙述配置信 ...

  5. HTML5开发移动web应用——SAP UI5篇(7)

    SAPUI5中支持利用Component对组件进行封装.想封装一个组件,Component的基本代码例如以下: sap.ui.define([ "sap/ui/core/UIComponen ...

  6. AFNetworking 3.0携带參数上传文件Demo

    一.服务端代码: 服务端是java用国产nutz搞的,实际mvc框架都大同小异.就是提交文件的同一时候还带了个表单參数 @AdaptBy(type=UploadAdaptor.class, args= ...

  7. mysql数据库字符编码修改

    mysql数据库字符编码修改 修改数据库的字符集mysql>use mydb mysql>alter database mydb character set utf8; 创建数据库指定数据 ...

  8. THRDTERM-----干净地结束一个线程

    THRDTERM产生两个线程.周期性地检查一个event对象.以决定要不要结束自己. #define WIN32_LEAN_AND_MEAN #include<stdio.h> #incl ...

  9. BZOJ 2440 中山市选2011 全然平方数 二分答案+容斥原理+莫比乌斯反演

    题目大意:求第k个无平方因子数是多少(无视原题干.1也是全然平方数那岂不是一个数也送不出去了? 无平方因子数(square-free number),即质因数分解之后全部质因数的次数都为1的数 首先二 ...

  10. 87.Ext_菜单组件_Ext.menu.Menu

    转自:https://blog.csdn.net/lms1256012967/article/details/52574921 菜单组件常用配置: /* Ext.menu.Menu主要配置项表: it ...