2070 爱情之路
2016-10-27 16:45
246 查看
2070 爱情之路
时间限制: 1 s
空间限制: 128000 KB
题目等级 : 钻石 Diamond
题解
查 看运行结果
题目描述 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
分类标签 Tags 点此展开
SPFA 图论
题解:
限制型最短路(我起的名字)
设两个状态。
L,O,V,E分别与1、2、3、4对应
dis[i][j(1~4)]表示走到i点时的字 母为j的最短路
注意更新时,要用“>=”。因为路径长度相同时, 考验数会增多
AC代码:
#include<cstdio> #include<cstring> #include<queue> #include<iostream> using namespace std; inline const int read(){ register int x=0,f=1; register char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} return x*f; } inline const char in(){ for(register char ch=getchar();;ch=getchar()) if(ch>='A'&&ch<='Z') return ch; } const int N=1e4+10; struct ss{ int v,w,logo,next; }e[N<<4]; struct node{ int id,logo; node(int id=0,int logo=0):id(id),logo(logo){} }; int n,m,tot,dis [5],cnt [5],head ; bool vis [5]; void add(int x,int y,int z,int t){ e[++tot].v=y; e[tot].w=z; e[tot].logo=t; e[tot].next=head[x]; head[x]=tot; } void spfa(){ memset(dis,0x3f3f3f3f,sizeof dis); queue<node>q; q.push(node(1,0)); vis[1][0]=1; dis[1][0]=0; cnt[1][0]=0; while(!q.empty()){ node h=q.front();q.pop(); vis[h.id][h.logo]=0; for(int i=head[h.id];i;i=e[i].next){ if(e[i].logo==(h.logo%4+1)&&dis[e[i].v][e[i].logo]>=dis[h.id][h.logo]+e[i].w){ dis[e[i].v][e[i].logo]=dis[h.id][h.logo]+e[i].w; cnt[e[i].v][e[i].logo]=cnt[h.id][h.logo]+1; if(!vis[e[i].v][e[i].logo]){ vis[e[i].v][e[i].logo]=1; q.push(node(e[i].v,e[i].logo)); } } } } if(dis [4]!=0x3f3f3f3f) printf("%d %d",dis [4],cnt [4]>>2); else puts("HOLY SHIT!"); } char c; int main(){ n=read();m=read(); for(int i=1,x,y,z,t;i<=m;i++){ x=read();y=read();z=read();c=in(); switch(c){ case 'L':t=1;break; case 'O':t=2;break; case 'V':t=3;break; case 'E':t=4;break; } add(x,y,z,t); add(y,x,z,t); } spfa(); return 0; }
相关文章推荐
- Codevs 2070 爱情之路
- codevs 2070 爱情之路
- 程序员的爱情之路
- POJ 2070 Filling Out the Team(我的水题之路——double比较)
- 李嘉诚的爱情之路
- 一个前端的爱情之路
- CYQ.Data 轻量数据层之路 应用示例二 在线聊天(六)
- 视频编解码学习之路(H264)
- 【白书之路】 1586 - Molar mass 字符串分离处理
- Android学习之路--四大组件--ContentProvider
- Hibernate的学习之路三十三(QBC的基本查询方式)
- Clannad——让我了解友情,亲情和爱情的动画片
- 【Java工程师之路】[1-2.7]Java面向对象详解6
- 数据结构学习之路-第三章:栈的应用
- java学习之路之基本语法-运算符练习题
- Hibernate的学习之路三十四(QBC的聚合函数的查询)
- CYQ.Data 轻量数据层之路 使用篇四曲 MAction 增删改(十五)
- 中国第一计算机编程高手横瓜的天才求职之路异常艰辛,天妒奇才呀
- AdaultBird--我的黑马程序员之路!Chapter2---异常体系,多线程,同步代码块和同步函数,死锁
- 未来之路