您的位置:首页 > 健康 > 养生 > 公司注册地址怎么查_免费网页在线客服系统甜甜_网站设计专业的公司_百度搜索技巧

公司注册地址怎么查_免费网页在线客服系统甜甜_网站设计专业的公司_百度搜索技巧

2025/7/15 3:13:36 来源:https://blog.csdn.net/2401_88085478/article/details/148794291  浏览:    关键词:公司注册地址怎么查_免费网页在线客服系统甜甜_网站设计专业的公司_百度搜索技巧
公司注册地址怎么查_免费网页在线客服系统甜甜_网站设计专业的公司_百度搜索技巧

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≤N≤500), 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

题目大意:给出当前所在位置和目的地,根据地图可以计算出最短路线。现有 n 个路口,m 条边,每条从 v1 到 v2 的边的标记为 0 时,代表该边是双向边,否则为 v1 到 v2 的单向边。每条边有路径长度和花费时间,问从源点到终点的最短长度的路径和最少花费时间的路径分别是什么。对于最短路径长度,如果有多条,则选择时间最短的路径;对于最少花费时间,如果有多条,则选择经过路口最少得路径。保证结果唯一。

分析:用 dijkstra 算法分别求出长度和时间的最短路径,以及最短路的前驱节点,之后分别进行 DFS 求符合要求的路径。

求最短路径可以用一个 dijkstra 函数解决,只需要改变传入的参数;进行 dfs 求路径时,注意比较的值不同。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define INF 0x3fffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
#define NUMBER_OF_THREADS   10
using namespace std;void dijkstra(int s,int n,int dis[],vector<int>pre[],int **graph)
{int flag[n+5]={0};dis[s]=0;for(int times=0;times<n;++times){int u=-1,mini=INF;for(int i=0;i<n;++i){if(!flag[i]&&dis[i]<mini)u=i,mini=dis[i];}if(u==-1)return;flag[u]=1;for(int i=0;i<n;++i){if(!flag[i]&&graph[u][i]!=INF){if(dis[i]>dis[u]+graph[u][i]){dis[i]=dis[u]+graph[u][i];pre[i].clear();pre[i].push_back(u);}else if(dis[i]==dis[u]+graph[u][i])pre[i].push_back(u);}}}
}void dfs_len(int s,int d,int&sum_len,int&sum_time,int **graph_length,int **graph_time,vector<int>pre[],vector<int>&temp,vector<int>&ans)
{if(d==s){int len=0,time=0,ind=s;for(int i=temp.size()-1;i>=0;--i){len+=graph_length[ind][temp[i]],time+=graph_time[ind][temp[i]];ind=temp[i];}if((len<sum_len)||(len==sum_len&&time<sum_time))ans=temp,sum_len=len,sum_time=time;return;}for(int i=0;i<pre[d].size();++i){temp.push_back(d);dfs_len(s,pre[d][i],sum_len,sum_time,graph_length,graph_time,pre,temp,ans);temp.pop_back();}return;
}void dfs_time(int s,int d,int&sum_time,int **graph,vector<int>pre[],vector<int>&temp,vector<int>&ans)
{if(d==s){int len=0,time=0,ind=s;for(int i=temp.size()-1;i>=0;--i){time+=graph[ind][temp[i]];ind=temp[i];}if((time<sum_time)||(time==sum_time&&temp.size()<ans.size()))ans=temp,sum_time=time;return;}for(int i=0;i<pre[d].size();++i){temp.push_back(d);dfs_time(s,pre[d][i],sum_time,graph,pre,temp,ans);temp.pop_back();}return;
}int main(void)
{#ifdef testfreopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);clock_t start=clock();#endif //testint n,m;scanf("%d%d",&n,&m);int dis_leng[n+5],dis_time[n+5];int **graph_len=(int**)malloc(sizeof(int*)*(n+5));int **graph_time=(int**)malloc(sizeof(int*)*(n+5));for(int i=0;i<n;++i){dis_leng[i]=dis_time[i]=INF;graph_len[i]=(int*)malloc(sizeof(int)*(n+5));graph_time[i]=(int*)malloc(sizeof(int)*(n+5));for(int j=0;j<n;++j)graph_len[i][j]=graph_time[i][j]=INF;}for(int i=0;i<m;++i){int v1,v2,flag,length,time;scanf("%d%d%d%d%d",&v1,&v2,&flag,&length,&time);graph_len[v1][v2]=length,graph_time[v1][v2]=time;if(!flag)graph_len[v2][v1]=length,graph_time[v2][v1]=time;}int s,d;scanf("%d%d",&s,&d);vector<int>pre_length[n+5],pre_time[n+5];dijkstra(s,n,dis_leng,pre_length,graph_len);dijkstra(s,n,dis_time,pre_time,graph_time);vector<int>temp,ans_len,ans_time;int sum_time=INF,sum_length=INF,sum_ind=INF;dfs_len(s,d,sum_length,sum_time,graph_len,graph_time,pre_length,temp,ans_len);temp.clear();sum_time=INF;dfs_time(s,d,sum_time,graph_time,pre_time,temp,ans_time);printf("Distance = %d",sum_length);if(ans_time==ans_len){printf("; Time = %d: %d",sum_time,s);for(int i=ans_time.size()-1;i>=0;--i)printf(" -> %d",ans_time[i]);printf("\n");}else{printf(": %d",s);for(int i=ans_len.size()-1;i>=0;--i)printf(" -> %d",ans_len[i]);printf("\n");printf("Time = %d: %d",sum_time,s);for(int i=ans_time.size()-1;i>=0;--i)printf(" -> %d",ans_time[i]);printf("\n");}for(int i=0;i<n;++i){free(graph_len[i]);free(graph_time[i]);}free(graph_len);free(graph_time);#ifdef testclockid_t end=clock();double endtime=(double)(end-start)/CLOCKS_PER_SEC;printf("\n\n\n\n\n");cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位#endif //testreturn 0;
}

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com