zoj 2504 help john
2010-08-28 23:33
239 查看
ZOJ Problem Set - 2504
Help John!
Time Limit:
1 Second
Memory Limit:
32768 KB
John is 7 years old and he lives in
Faegfalch, which is a very dangerous city, so John must go to school
according to the path his mother told him. This way isn't the fastest
way from John's home to school. John is a very lazy boy and he doesn't
like walking, so he is searching for the fastest way from home to
school. He only has to make sure, that his mother can not see from her
flat window that he is going on another way (he must pass the first
crossroad where mother told him to go across, and never walk back home
unless he comes back from school).
INPUT
First line of input includes t
(1<=t
<=77) - the number of test cases.
First line of every test includes n
(1<=n
<=1 000) - the number of crossroads in Faegfalch and m
(1<=m
<=30 000) - the number of roads. In next m
lines there are descriptions of roads. Every road description includes three numbers: a
, b
(1<=a
, b
<=n
) - crossroads, where the road starts and ends and c
(1<=c
<=1
000) - the time needed to walk by this street (John can't walk faster,
because if he did it, he would be cought by Faegnad (football team) fans
and he can't go slower, because he doesn't want to be late and he wants
to talk with his friends and make some homework before lessons start).
In next line there is an integer k
(1<=k
<=m
) - the number of crossroads, which his mother told him to meet. Next line includes k
crossroads which he has to meet.
The boy's home is always at location 1 and the school positions at location n
. All the paths are bidirectional.
OUTPUT
Output has one line for every test, which include word "TEST" and then
the order number. Print the character "Y" if John can go to his school
from home, and the time needs to go to school by roads which his mother
told him to go, subtracts the minimal time which he needs to walk to
school that can not be seen by his mother. Output only an "N" if there
is no way to his school.
SAMPLE
INPUT
2
6 10
1 2 2
1 3 2
1 6 1
2 6 4
2 3 3
2 4 1
3 4 5
4 5 1
4 6 3
5 6 2
6
1 2 3 4 5 6
3 1
1 2 1
3
1 2 3
(
Explanation for the first sample.
the way his mother told him is 1->2->3->4->5->6, with a
total length of 13. And the shortest path he can find is 1->2->6,
with a total length of 6. So the final output is 13 - 6 = 7.)
OUTPUT
TEST 1 Y 7
TEST 2 N
这题的题意就是john上学,她妈妈给他规定的有路线,但是只有第一条路必须按照他妈妈的规定的路线,其他的可以自己掌握,问看看有没有比她妈妈说的路线更短的线路,如果有,则输出抄近道节省下来的时间,如果不能到达,就输出N;
这个刚开始用的floyd做的,超时,后来无奈之下改成dij,不写不知道,一写郁闷了,发现不会写dij了,于是决定先把前边写过的最短路径改改,改成dij的练习练习,反正,后边的题也不会写。
INT_MAX 定义小了,wa了几次。
Help John!
Time Limit:
1 Second
Memory Limit:
32768 KB
John is 7 years old and he lives in
Faegfalch, which is a very dangerous city, so John must go to school
according to the path his mother told him. This way isn't the fastest
way from John's home to school. John is a very lazy boy and he doesn't
like walking, so he is searching for the fastest way from home to
school. He only has to make sure, that his mother can not see from her
flat window that he is going on another way (he must pass the first
crossroad where mother told him to go across, and never walk back home
unless he comes back from school).
INPUT
First line of input includes t
(1<=t
<=77) - the number of test cases.
First line of every test includes n
(1<=n
<=1 000) - the number of crossroads in Faegfalch and m
(1<=m
<=30 000) - the number of roads. In next m
lines there are descriptions of roads. Every road description includes three numbers: a
, b
(1<=a
, b
<=n
) - crossroads, where the road starts and ends and c
(1<=c
<=1
000) - the time needed to walk by this street (John can't walk faster,
because if he did it, he would be cought by Faegnad (football team) fans
and he can't go slower, because he doesn't want to be late and he wants
to talk with his friends and make some homework before lessons start).
In next line there is an integer k
(1<=k
<=m
) - the number of crossroads, which his mother told him to meet. Next line includes k
crossroads which he has to meet.
The boy's home is always at location 1 and the school positions at location n
. All the paths are bidirectional.
OUTPUT
Output has one line for every test, which include word "TEST" and then
the order number. Print the character "Y" if John can go to his school
from home, and the time needs to go to school by roads which his mother
told him to go, subtracts the minimal time which he needs to walk to
school that can not be seen by his mother. Output only an "N" if there
is no way to his school.
SAMPLE
INPUT
2
6 10
1 2 2
1 3 2
1 6 1
2 6 4
2 3 3
2 4 1
3 4 5
4 5 1
4 6 3
5 6 2
6
1 2 3 4 5 6
3 1
1 2 1
3
1 2 3
(
Explanation for the first sample.
the way his mother told him is 1->2->3->4->5->6, with a
total length of 13. And the shortest path he can find is 1->2->6,
with a total length of 6. So the final output is 13 - 6 = 7.)
OUTPUT
TEST 1 Y 7
TEST 2 N
这题的题意就是john上学,她妈妈给他规定的有路线,但是只有第一条路必须按照他妈妈的规定的路线,其他的可以自己掌握,问看看有没有比她妈妈说的路线更短的线路,如果有,则输出抄近道节省下来的时间,如果不能到达,就输出N;
这个刚开始用的floyd做的,超时,后来无奈之下改成dij,不写不知道,一写郁闷了,发现不会写dij了,于是决定先把前边写过的最短路径改改,改成dij的练习练习,反正,后边的题也不会写。
INT_MAX 定义小了,wa了几次。
#include <stdio.h> #define INT_MAX 99999990 int map[1005][1005],told[30001],hash[1005],dist[1005]; int main (void) { int N,i,j,k,n,now,min; int from,to,cost; scanf("%d",&N); for (i=1;i<=N;i++) { int ncase,m; scanf("%d %d",&ncase,&m); for (j=1;j<=ncase;j++) { for (k=1;k<=ncase;k++) map[j][k] = INT_MAX; map[j][j] = 0; dist[j] = INT_MAX; hash[j] = 0; } for (j=1;j<=m;j++) { scanf("%d %d %d",&from,&to,&cost); map[from][to] = map[to][from]= cost; } scanf("%d",&n); for (j=1;j<=n;j++) scanf("%d",&told[j]); int sum = 0; for (j=1;j<n;j++) sum+=map[ told[j] ][ told[j+1] ]; now = told[2];hash[now] = 1;dist[now] = map[1][told[2]]; for (j=2;j<=ncase;j++) { for (k=2;k<=ncase;k++) if (!hash[k]&& dist[k] > dist[now] +map[now][k]) dist[k] = dist[now] + map[now][k]; for (k=2,min=INT_MAX;k<=ncase;k++) { if (!hash[k]&&min>dist[k]) min = dist[now=k]; } hash[now] = 1; } printf("TEST %d ",i); m = dist[ncase]; if (m>= INT_MAX) printf("N/n"); else printf("Y %d/n",sum-m); } return 0; }
相关文章推荐
- ZOJ 2504 Help John! (dijkstra)
- ZOJ 2504 Help John! 【最短路】 【Dijkstra】
- ZOJ2504 Help John!,Floyd算法会TLE,只能用Dijkstra算法了
- zoj 2504 Help John!(Dijkstra~~)
- ZOJ 2504 Help John!
- 简单概率dp(期望)-zoj-3640-Help Me Escape
- zoj 3640 Help Me Escape 概率dp+dfs记忆化
- zoj 3640 Help Me Escape (概率与期望DP)
- zoj 3640 Help Me Escape (概率dp)
- ZOJ 3167 Find 7 Faster Than John Von Neumann
- zoj 3640 Help Me Escape 概率DP
- zoj-3640-Help Me Escape-概率DP
- zoj3882 Help Bob 博弈
- zoj 3167 Find 7 Faster Than John Von Neumann
- ZOJ 1712 ACboy needs your help(分组背包)
- ZOJ 3640 Help Me Escape [概率DP]
- zoj 3882 Help Bob(zoj 2015年7月月赛)
- ZOJ-3640 Help Me Escape 概率DP
- zoj 2504
- ZOJ 3640 Help Me Escape