[PTA]L2-001 紧急救援 (25 分)
L2-001 紧急救援 (25 分)
Description
作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上。当其他城市有紧急求助电话给你的时候,你的任务是带领你的救援队尽快赶往事发地,同时,一路上召集尽可能多的救援队。
Input
输入第一行给出4个正整数N、M、S、D,其中N(2)是城市的个数,顺便假设城市的编号为0 ~ (;M是快速道路的条数;S是出发地的城市编号;D是目的地的城市编号。
第二行给出N个正整数,其中第i个数是第i个城市的救援队的数目,数字间以空格分隔。随后的M行中,每行给出一条快速道路的信息,分别是:城市1、城市2、快速道路的长度,中间用空格分开,数字均为整数且不超过500。输入保证救援可行且最优解唯一。
output
第一行输出最短路径的条数和能够召集的最多的救援队数量。第二行输出从S到D的路径中经过的城市编号。数字间以空格分隔,输出结尾不能有多余空格。
Examples
Input
4 5 0 3
20 30 40 10
0 1 1
1 3 2
0 3 3
0 2 2
2 3 2
Output
2 60
0 1 3
正确解法:
跟最短路计数一样。用的是dijkstra。
一个计数,如果新路径,就相等。相同的最短路就相加。
一个最多数目。如果新路经就相加。相同的就更新最大值。并且更新前缀点。
求路径就是记录一个前缀点。
1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<map> 6 #include<set> 7 #include<vector> 8 #include<queue> 9 #include<algorithm> 10 //#include<cmath> 11 using namespace std; 12 typedef long long ll; 13 const int inf=0x7fffffff; 14 const int N=1001; 15 const int M=9999999; 16 const ll mod=1000000000+7; 17 int n,m,s,d,len=0; 18 int Link[N],dis[N],con[N],num[N],val[N],pre[N],bok[N]; 19 int ans[N],cnt=0; 20 struct node 21 { 22 int y,v,next; 23 }e[M]; 24 void insert(int xx,int yy,int vv) 25 { 26 e[++len].next=Link[xx]; 27 Link[xx]=len; 28 e[len].y=yy; 29 e[len].v=vv; 30 } 31 void init() 32 { 33 scanf("%d %d %d %d",&n,&m,&s,&d); 34 for(int i=0;i<n;i++) 35 { scanf("%d",&val[i]); 36 dis[i]=inf; 37 pre[i]=-1; 38 num[i]=val[i]; 39 } 40 for(int i=1;i<=m;i++) 41 { 42 int xx,yy,vv; 43 scanf("%d %d %d",&xx,&yy,&vv); 44 insert(xx,yy,vv); 45 insert(yy,xx,vv); 46 } 47 dis[s]=0; 48 bok[s]=1; 49 con[s]=1; 50 for(int i=Link[s];i;i=e[i].next) 51 { dis[e[i].y]=e[i].v; 52 con[e[i].y]=1; 53 } 54 for(int i=1;i<n;i++) 55 { 56 int minn=inf,u=s; 57 for(int j=0;j<n;j++) 58 if(bok[j]==0&&dis[j]<minn) 59 { 60 minn=dis[j]; 61 u=j; 62 } 63 bok[u]=1; 64 // cout<<u<<endl; 65 for(int j=Link[u];j;j=e[j].next) 66 { 67 if(dis[e[j].y]>dis[u]+e[j].v) 68 { 69 dis[e[j].y]=dis[u]+e[j].v; 70 con[e[j].y]=con[u]; 71 num[e[j].y]=num[u]+val[e[j].y]; 72 pre[e[j].y]=u; 73 } 74 else if(dis[e[j].y]==dis[u]+e[j].v) 75 { 76 con[e[j].y]+=con[u]; 77 if(num[e[j].y]<num[u]+val[e[j].y]) 78 { 79 num[e[j].y]=num[u]+val[e[j].y]; 80 pre[e[j].y]=u; 81 } 82 } 83 } 84 //cout<<num[3]<<endl; 85 86 } 87 88 } 89 int main() 90 { 91 init(); 92 cout<<con[d]<<" "<<num[d]+num[s]<<endl; 93 for(int i=d;i!=-1;i=pre[i]) 94 ans[++cnt]=i; 95 printf("%d",s); 96 for(int i=cnt;i>0;i--) 97 printf(" %d",ans[i]); 98 printf("\n"); 99 100 return 0; 101 }
View Code
等会有spfa写一下QAQ
错了一个数据的spfa()
1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<map> 6 #include<set> 7 #include<vector> 8 #include<queue> 9 #include<algorithm> 10 //#include<cmath> 11 using namespace std; 12 typedef long long ll; 13 const int inf=0x7fffffff; 14 const int N=1001; 15 const int M=9999999; 16 const ll mod=1000000000+7; 17 int n,m,s,d,len=0; 18 int Link[N],dis[N],con[N],num[N],val[N],pre[N],bok[N]; 19 int que[M+100]; 20 int ans[N],cnt=0; 21 struct node 22 { 23 int y,v,next; 24 }e[M]; 25 void insert(int xx,int yy,int vv) 26 { 27 e[++len].next=Link[xx]; 28 Link[xx]=len; 29 e[len].y=yy; 30 e[len].v=vv; 31 } 32 void init() 33 { 34 scanf("%d %d %d %d",&n,&m,&s,&d); 35 for(int i=0;i<n;i++) 36 { scanf("%d",&val[i]); 37 dis[i]=inf; 38 pre[i]=-1; 39 num[i]=val[i]; 40 } 41 for(int i=1;i<=m;i++) 42 { 43 int xx,yy,vv; 44 scanf("%d %d %d",&xx,&yy,&vv); 45 insert(xx,yy,vv); 46 insert(yy,xx,vv); 47 } 48 dis[s]=0; 49 bok[s]=1; 50 con[s]=1; 51 for(int i=Link[s];i;i=e[i].next) 52 { 53 //dis[e[i].y]=e[i].v; 54 con[e[i].y]=1; 55 } 56 int head=1,tail=2; 57 que[1]=s; 58 while(head<tail) 59 { 60 int tt=que[head]; 61 //cout<<tt<<" "<<num[3]<<endl; 62 bok[tt]=0; 63 for(int i=Link[tt];i;i=e[i].next) 64 { 65 if(dis[e[i].y]>dis[tt]+e[i].v) 66 { 67 dis[e[i].y]=dis[tt]+e[i].v; 68 con[e[i].y]=con[tt]; 69 num[e[i].y]=num[tt]+val[e[i].y]; 70 pre[e[i].y]=tt; 71 if(bok[e[i].y]==0) 72 { 73 que[tail++]=e[i].y; 74 bok[e[i].y]=1; 75 } 76 } 77 else if(dis[e[i].y]==dis[tt]+e[i].v) 78 { 79 con[e[i].y]+=con[tt]; 80 if(num[e[i].y]<num[tt]+val[e[i].y]) 81 { 82 num[e[i].y]=num[tt]+val[e[i].y]; 83 pre[e[i].y]=tt; 84 } 85 } 86 } 87 head++; 88 } 89 } 90 int main() 91 { 92 init(); 93 cout<<con[d]<<" "<<num[d]<<endl; 94 for(int i=d;i!=-1;i=pre[i]) 95 ans[++cnt]=i; 96 //printf("%d",s); 97 for(int i=cnt;i>1;i--) 98 printf("%d ",ans[i]); 99 printf("%d\n",ans[1]); 100 101 return 0; 102 }
View Code
转载于:https://www.cnblogs.com/Kaike/p/10622518.html
[PTA]L2-001 紧急救援 (25 分)相关推荐
- 5-35 城市间紧急救援 (25分) pat 数据结构
题目连接 https://pta.patest.cn/pta/test/15/exam/4/question/862 5-35 城市间紧急救援 (25分) 作为一个城市的应急救援队伍的负责人,你有 ...
- 天梯赛:L2-001 紧急救援 (25 分)
题目详情 - L2-001 紧急救援 (25 分) (pintia.cn) 题解:使用dijkstra算法就可以了,难点在于如何存储路径.我首先十分单纯的用了pair<int,int>进行 ...
- 【CCCC】L2-001 紧急救援 (25分),,Dijkstra标准模板(多路径,最大点权和路径打印)
problem L2-001 紧急救援 (25分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两 ...
- 天梯赛 L2-001 紧急救援 (25 分)
单元最短路+最短路数量+最大点权和+输出路径 L2-001 紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每 ...
- PTA7-12 城市间紧急救援 (25 分)(dijkstra+dp)(简单易懂的写法)
PTA7-12 城市间紧急救援 (25 分) 关于这个题呢,我当时也想了很久没有一点头绪.所以,菜鸡的我一如既往的打开了CSDN(手动滑稽).我看了很多大佬的写法,最普遍的应该就是dijkstra+d ...
- 7-11 城市间紧急救援 (25 分)
7-11 城市间紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速 ...
- 紧急救援 (25 分)
L2-001 紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道 ...
- PTA L2-2 冰岛人 (25分)
PTA L2-2 冰岛人 (25分) 2018年世界杯,冰岛队因1:1平了强大的阿根廷队而一战成名.好事者发现冰岛人的名字后面似乎都有个"松"(son),于是有网友科普如下: 冰岛 ...
- 5-35 城市间紧急救援 (25分)
5-35 城市间紧急救援 (25分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道 ...
- L2-001 紧急救援 (25 分)最短路径 迪杰斯特拉算法
L2-001 紧急救援 题目 代码 题目 L2-001 紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城 ...
最新文章
- condest--1-范数的条件数估计
- Linux常用的基本命令head、tail、tar、grep、date、cal(二)
- UGUI_UGUI事件系统简述及使用方法总结
- Multipath多路径冗余全解
- 我的Java之路(7)
- 删除桌面小箭头小盾牌
- python读取ini文件
- python作用域总结_关于Python作用域自学总结
- 无法进入一个空框_DeNoise AI无法从Photoshop作为插件启动?
- 新浪微博与腾讯微博的开放平台比较 -- 从程序员的角度
- 10 GridView 样式属性
- Android定制实现上网限制
- windows下删除文件:提示无法删除文件,无法读源文件或磁盘
- C语言复健(数组) 珠心算测验
- 关于类加载机制,你知道多少
- 小白思路理顺U-Net模型(keras)--预测部分
- html标签:表格、列表、图片、文字、表单、以及h5新增特性
- 超级生产力的背后是强大的技术实力 央媒点赞小米“黑灯工厂”
- 基于tensorflow2的手写中文数字识别(自己创建数据集)
- 自动更换Termux的源,加快软件包下载速度笔记