# pta 天梯地图 （Dijkstra）

### 输入格式：

`V1 V2 one-way length time`

### 输出格式：

`Time = T: 起点 => 节点1 => ... => 终点`

`Distance = D: 起点 => 节点1 => ... => 终点`

`Time = T; Distance = D: 起点 => 节点1 => ... => 终点`

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

### 输出样例1：

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

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

### 输出样例2：

`Time = 3; Distance = 4: 3 => 2 => 5`
`Dijkstra ，条件比较多，最短路和最短时间两次分开就行。`
```#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <map>
#include <queue>
using namespace std;
const int maxn=1e5;
struct Node
{
int value;
int next;
int dis;
int time;
}edge[maxn*4+5];
int tot;
void join(int x,int y,int d,int t)
{
edge[tot].value=y;
edge[tot].dis=d;
edge[tot].time=t;
}
int vis[maxn+5];
int ans,res;
struct node
{
int x;
int dis;
int num;
int time;
node(){};
node(int x,int dis,int num,int time)
{
this->x=x;
this->dis=dis;
this->num=num;
this->time=time;
}
friend bool operator <(node a,node b)
{
return a.dis>b.dis;
}
};
int d[maxn+5];
int d2[maxn+5];
int num[maxn+5];
int t[maxn+5];
int ansd[maxn+5];
int anst[maxn+5];
int n;
int v1,v2;
void Dijkstra(int v1,int v2)
{
priority_queue<node> q;
for(int i=0;i<=n;i++)
d[i]=t[i]=1e9;
memset(vis,0,sizeof(vis));
num[v1]=0;
d[v1]=t[v1]=0;
ansd[v1]=-1;
anst[v1]=-1;
while(!q.empty())
q.pop();
q.push(node(v1,0,0,0));
while(!q.empty())
{
node term=q.top();
q.pop();
if(term.x==v2)
{
break;
}
{
int v=edge[i].value;

if(d[v]>term.dis+edge[i].dis)
{
d[v]=term.dis+edge[i].dis;
num[v]=term.num+1;
q.push(node(v,d[v],term.num+1,0));
ansd[v]=term.x;
}
else if(d[v]==term.dis+edge[i].dis)
{
if(num[v]>term.num+1)
{
num[v]=term.num+1;
ansd[v]=term.x;
q.push(node(v,d[v],term.num+1,0));
}
}
}
}
while(!q.empty())
q.pop();
q.push(node(v1,0,0,0));
d2[v1]=0;
while(!q.empty())
{
node term=q.top();
q.pop();
if(term.x==v2)
{
break;
}
{
int v=edge[i].value;
if(t[v]>term.dis+edge[i].time)
{
t[v]=term.dis+edge[i].time;
d2[v]=term.time+edge[i].dis;
q.push(node(v,t[v],0,d2[v]));
anst[v]=term.x;
}
else if(t[v]==term.dis+edge[i].time)
{
if(d2[v]>term.time+edge[i].dis)
{
d2[v]=term.time+edge[i].dis;
q.push(node(v,t[v],0,d2[v]));

anst[v]=term.x;
}
}
}
}

}
int fun3(int x)
{
if(x==-1)
return 1;
if(ansd[x]!=anst[x])
return 0;
return fun3(ansd[x]);
}
void fun(int x)
{
if(x==-1)
return;
fun(ansd[x]);
if(x==v2)
printf("%d",x);
else
printf("%d => ",x);
}
void fun2(int x)
{
if(x==-1)
return;
fun2(anst[x]);
if(x==v2)
printf("%d",x);
else
printf("%d => ",x);
}
int m,k;
int main()
{
scanf("%d%d",&n,&m);
int x,y,dd,tt,ta;
memset(vis,0,sizeof(vis));
tot=0;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d%d%d",&x,&y,&ta,&dd,&tt);
join(x,y,dd,tt);
if(ta==0)
join(y,x,dd,tt);
}
scanf("%d%d",&v1,&v2);
Dijkstra(v1, v2);
if(fun3(v2)==1)
{
printf("Time = %d; Distance = %d: ",t[v2],d[v2]);
fun2(v2);
printf("\n");
return 0;
}
printf("Time = %d: ",t[v2]);
fun2(v2);
printf("\n");
printf("Distance = %d: ",d[v2]);
fun(v2);
printf("\n");

return 0;

}```

479 篇文章43 人订阅

0 条评论

## 相关文章

### 3. call PRXSUBSTR () | 庖丁解牛切割数据！

【SAS Says·扩展篇】庖丁解牛割数据！ | 3. call PRXSUBSTR () 0. 前集回顾 1. 新的问题 2. 初识 PRXSUBSTR() ...

3585

42219

### java基础学习_面向对象(上)01_day07总结

============================================================================= ==...

1002

5344

902

3492

8985

### 面向对象VS面向过程

1 前言 向伟人致敬 其实这个问题真的是被问烂了，特别是刚入门的同行，我感觉这个问题应该是大家都听说过了，但是有多少人真的是理解这两个区别呢，这两...

2115

2932

1262