一个人的旅行
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 25159 Accepted Submission(s): 8743
Problem Description
虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景……草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽约纯粹看雪景,去巴黎喝咖啡写信,去北京探望孟姜女……眼看寒假就快到了,这么一大段时间,可不能浪费啊,一定要给自己好好的放个假,可是也不能荒废了训练啊,所以草儿决定在要在最短的时间去一个自己想去的地方!因为草儿的家在一个小镇上,没有火车经过,所以她只能去邻近的城市坐火车(好可怜啊~)。
Input
输入数据有多组,每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个,草儿想去的地方有D个;
接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车程是time小时;(1=<(a,b)<=1000;a,b 之间可能有多条路)
接着的第T+1行有S个数,表示和草儿家相连的城市;
接着的第T+2行有D个数,表示草儿想去地方。
Output
输出草儿能去某个喜欢的城市的最短时间。
Sample Input
6 2 3
1 3 5
1 4 7
2 8 12
3 8 4
4 9 12
9 10 2
1 2
8 9 10
Sample Output
为何感觉这个dijkstra 跟之前的有些不同,或许是起点有多个~~
dijkstra:
#include<cstdio>
#include<cstring>
#include<cmath>
#define Wi(a) while(a--)
#define Si(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define Pi(a) printf("%d\n", (a))
#define mem(a, b) memset(a, (b), sizeof(a))
#define INF 0x3f3f3f
#include<algorithm>
using namespace std;
const int mx = 1010;
int road, nearcity, want, n;
int map[mx][mx];
int dis[mx], vis[mx];
void init()
{
for(int i = 0; i < mx; i++)
{
for(int j = 0; j < mx; j++)
{
map[i][j] = (i == j ? 0 : INF);
}
}
}
void getmap()
{
int a, b, c;
n = 0;
Wi(road){
Si(a, b, c);
if(map[a][b] > c)
map[a][b] = map[b][a] = c;
n = max(max(a, b), n);
}
Wi(nearcity){
scanf("%d", &a);
map[a][0] = map[0][a] = 0;
}
}
void dijkstra(int s)
{
int i, j, k;
int minn;
mem(vis, 0);
for(i = 1; i <= n; i++) dis[i] = map[s][i];
dis[s] = 0;
vis[s] = 1;
for(i = 1; i < n; i++)
{
minn = INF;
for(j = 1; j <= n; j++)
{
if(!vis[j] && dis[j] < minn)
{
minn = dis[j], k = j;
}
}
if(minn == INF ) break;
vis[k] = 1;
for(j = 1; j <= n; j++)
{
if(!vis[j]) dis[j] = min(dis[j], dis[k]+map[k][j]);
}
}
}
void solve()
{
int ans = INF;
Wi(want){
int a;
scanf("%d", &a);
if(ans > dis[a]) ans = dis[a];
}
Pi(ans);
}
int main()
{
while(Si(road, nearcity, want)==3)
{
init();
getmap();
dijkstra(0);
solve();
}
return 0;
}
floyd:
#include<cstdio>
#include<cstring>
#include<cmath>
#define Wi(a) while(a--)
#define Si(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define Pi(a) printf("%d\n", (a))
#define mem(a, b) memset(a, (b), sizeof(a))
#define INF 0x3f3f3f
#include<algorithm>
using namespace std;
const int mx = 1010;
int road, nearcity, want, n;
int map[mx][mx];
int dis[mx], vis[mx];
int s[mx], t[mx];
void init()
{
for(int i = 0; i < mx; i++)
{
for(int j = 0; j < mx; j++)
{
map[i][j] = (i == j ? 0 : INF);
}
}
}
void getmap()
{
int a, b, c;
n = 0;
Wi(road){
Si(a, b, c);
if(map[a][b] > c)
map[a][b] = map[b][a] = c;
n = max(max(a, b), n);
}
mem(s, 0); mem(t, 0);
for(int i = 0; i < nearcity; i++) scanf("%d", &s[i]);
for(int j = 0; j < want; j++) scanf("%d", &t[j]);
}
void floyd()
{
int i, j, k;
for(k = 0; k <= n; k++)
{
for(i = 0; i <= n; i++)
{
if(map[i][k] != INF)//不加会导致 TLE....
{
for(j = 0; j <= n; j++)
map[i][j] = min(map[i][j], map[i][k]+map[k][j]);
}
}
}
}
void solve()
{
int ans = INF;
for(int i = 0; i < nearcity; i++)
{
for(int j = 0; j < want; j++)
ans = min(ans, map[s[i]][t[j]]);
}
Pi(ans);
}
int main()
{
while(Si(road, nearcity, want)==3)
{
init();
getmap();
floyd();
solve();
}
return 0;
}
SPFA:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#define mem(a, b) memset(a, (b), sizeof(a))
#define Wi(a) while(a--)
#define Si(a, b, c) scanf("%d%d%d", &a,&b,&c)
#define Pi(a) printf("%d\n", (a))
#define INF 0x3f3f3f
#include<algorithm>
using namespace std;
const int mx = 1010;
const int mr = 10000+10;
int road, nearcity, want;
int dis[mx], vis[mx], use[mx];
int s[mx], t[mx];
bool flag;
struct node{
int from, to, val, next;
};
node edge[mr];
int head[mx], edgenum;
void add(int a, int b, int c)
{
node E = { a, b, c, head[a]};
edge[edgenum] = E;
head[a] = edgenum++;
}
void init()
{
edgenum = 0;
mem(head, -1);
}
void spfa(int s)
{
queue<int> q;
mem(dis, INF); mem(vis, 0); mem(use, 0);
q.push(s);
dis[s] = 0;
vis[s] = 1;
use[s]++;
while(!q.empty()){
int u = q.front(); q.pop(); vis[u] = 0;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(dis[v] > dis[u]+edge[i].val)
{
dis[v] = dis[u] + edge[i].val;
if(!vis[v]){
vis[v] = 1;
q.push(v);
}
}
}
}
}
void getmap()
{
int a, b, c;
Wi(road){
Si(a, b, c);
add(a, b, c);
add(b, a, c);
}
}
void solve()
{
int ans = INF;
for(int i = 0; i < nearcity; i++) scanf("%d", &s[i]);
for(int j = 0; j < want; j++) scanf("%d", &t[j]);
for(int i = 0; i < nearcity; i++){
spfa(s[i]);
for(int j = 0; j < want; j++){
if(ans > dis[t[j]]) ans = dis[t[j]];
}
}
Pi(ans);
}
int main()
{
while(Si(road, nearcity, want)==3)
{
init();
getmap();
solve();
}
return 0;
}