题解:P7655 [BalticOI 1996 Day 2] A FAST JOURNEY
这道题是道很好的最短路练习题。\ 这题我们可以用迪杰斯特拉,把他其中的标记数组改为三维的,第一维是点的编号,第二维是当天的时间,第三维是看他是奇数天还是偶数天。\ 其他的也是这样加一维或两维。\ 还要记得记录路径,但要记得倒着输出,不然会全错的。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e3+1;
struct edge{
int v,w,dir;
};
struct node{
int id,day,time;
bool operator<(const node &x) const{
if(day==x.day)return time>x.time;
return day>x.day;
}
};
struct point{
int day,time;
}dist[N];
struct stu{
int u,day,w;
}pre[N];
int n,m,st,en;
vector<edge> g[N];
bool ok[N][N][2];
void dijkstra(){
priority_queue<node> pq;
memset(ok,false,sizeof(ok));
for(int i=1; i<=n; ++i){
dist[i].day=1e18;
dist[i].time=1e18;
}
dist[st].day=0;
dist[st].time=0;
pq.push({st,1,0});
while(pq.size()){
node now=pq.top();
pq.pop();
int u=now.id,day=now.day,time=now.time;
if(u==en)return;
if(ok[u][time][day&1])continue;
ok[u][time][day&1]=true;
for(auto it:g[u]){
int v=it.v,w=it.w,dir=it.dir;
if(time+w<=12 && dir==(day&1) && !ok[v][time+w][dir] && (day<dist[v].day || day==dist[v].day && time+w<dist[v].time)){
pre[v].day=day;
pre[v].w=w;
pre[v].u=u;
dist[v].day=day;
dist[v].time=time+w;
pq.push({v,day,time+w});
}
}
if(!ok[u][0][day+1&1])pq.push({u,day+1,0});
}
}
void print(int op){
if(!pre[op].u)return;
print(pre[op].u);
printf("%lld %lld %lld %lld\n",pre[op].u,op,pre[op].day,pre[op].w);
}
signed main(){
scanf("%lld%lld%lld%lld",&st,&en,&n,&m);
for(int i=1; i<=m; ++i){
int u,v,w;
scanf("%lld%lld%lld",&u,&v,&w);
g[u].push_back({v,w,1});
g[v].push_back({u,w,0});
}
dijkstra();
print(en);
return 0;
}