[codevs2070]愛情之路 分層圖最短路
題目描述 Description
yh非常想念他的女朋友小y,於是他決定前往小y所在的那塊大陸。
小y所在的大陸共有n個城市,m條雙向路,每條路連線一個或兩個城市。經過一條路ei需要耗費時間ti。此外,每條路均有一個特定標識,為’L’,’O’,’V’,’E’,中的某個字母。yh從1號城市出發,前往位於n號城市的小y所在處。
為了考驗yh,小y規定,yh必須按照‘L’->’O’->’V’->’E’->’L’->’O’->’V’->’E’->…. 的順序選擇路,且所走的第一條路是’L’,最後一條路是’E’,每走完一個完整的’LOVE’算是通過一次考驗
在不違背小y要求的前提下,yh想花費最少的時間到達小y的所在地,同在此時間內完成最多次考驗。你能幫yh算出,他最少要花多久到達城市n,完成多少次考驗呢?
輸入描述 Input Description
第一行為兩個整數n,m表示有n個城市,m條雙向路。
第2行到第m+1行,每行有3個整數x,y,t和一個字元char,城市x,y之間有路,通過這條路花費的時間為t,這條路的特殊標誌為 char。
輸出描述 Output Description
輸出1行,兩個整數表示yh到達城市n花費的最少時間和該時間內通過的最多次考驗數,如果不能到達則輸出’HOLY SHIT!’
樣例輸入 Sample Input
【樣例輸入1】
4 4
1 2 1 L
2 1 1 O
1 3 1 V
3 4 1 E
【樣例輸入2】
4 4
1 2 1 L
2 3 1 O
3 4 1 V
4 1 1 E
樣例輸出 Sample Output
【樣例輸出1】
4 1
【樣例輸出2】
HOLY SHIT!
資料範圍及提示 Data Size & Hint
對於100%資料,1≤n≤1314,0≤M≤13520
思路:
將SPFA普通的dis陣列多開一維來表示更新該點當前距離的是哪條路
資料有毒瘤自環,記得特判
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#define INF 1061109567
using namespace std;
const int MAXN = 200000 + 50;
struct edge{
int f,t,v;
int c;
}l[MAXN << 1];
int n,m;
int head[MAXN],next[MAXN << 1],tot;
int dis[MAXN][5];
void init(int n){
for(int i = 1;i <= n;i ++){
head[i] = -1;
for(int j = 0;j <= 3;j ++){
dis[i][j] = INF;
}
}
}
void build(int f,int t,int v,int c){
l[++ tot] = (edge){f,t,v,c};
next[tot] = head[f];
head[f] = tot;
}
int a,b,c,d;
char x;
struct zt{
int c,num;
};
queue <zt> q;
bool used[MAXN][4];
int step[MAXN][4];
bool flag;
void spfa(int x){
dis[x][3] = 0;
used[x][3] = true;
q.push((zt){3,x});
while(!q.empty()){
zt u = q.front();
q.pop();
used[u.num][u.c] = false;
for(int i = head[u.num];i != -1;i = next[i]){
int cv = (u.c + 1)%4;
if(cv != l[i].c)continue;
flag = true;
int t = l[i].t;
if(dis[t][cv] >= dis[u.num][u.c] + l[i].v || !dis[t][cv]){
dis[t][cv] = dis[u.num][u.c] + l[i].v;
step[t][cv] = max(step[t][cv],step[u.num][u.c] + 1);
if(!used[t][cv])q.push((zt){cv,t});
used[t][cv] = true;
}
}
}
}
int main(){
scanf("%d%d",&n,&m);
init(n);
for(int i = 1;i <= n;i ++){
cin >> a >> b >> c >> x;
switch(x){
case 'L':d = 0;break;
case 'O':d = 1;break;
case 'V':d = 2;break;
case 'E':d = 3;break;
}
build(a,b,c,d);
build(b,a,c,d);
}
spfa(1);
if(dis[n][3] == INF || !flag){
printf("HOLY SHIT!");
return 0;
}
if(dis[n][3] == 0)dis[n][3] = 4,step[n][3] = 4;
printf("%d %d",dis[n][3],step[n][3]/4);
return 0;
}
wa了五次
原因:nm打反……
誰叫兩個樣例給的nm都一樣呢……gg