Time Limit: 1000MS |
Memory Limit: 30000K | |
Total Submissions: 11183 | Accepted: 4096 |
Description
Input
Output
Sample Input
3 2 1 2 2 3 2 3 1 2 1 2
Sample Output
0
题意:火车从一点开到另一点,轨道上有很多岔路口,每个路口都有好几个方向(火车能够选任意一个方向开),但是火车默认的是第一个指向的方向,如果选择别的方向需要 进行一次切换操作 ,给定一个起点一个终点 ,问最少进行几次 切换操作 能够 使 火车从起点到达终点 , 若无法到达输出“-1”。 思路:设默认路径边权为0,备选路径边权为1,求单源最短路即可。
dijkstra:0ms
#include <cstdio> #include <cstring> #include <algorithm> #define INF 0x3f3f3f #define MAX 100+10 using namespace std; int n, s, e; int dist[MAX], map[MAX][MAX], vis[MAX]; void init() { for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(i == j) map[i][j] = 0; else map[i][j] = INF; } } } void getmap() { int num; int i, j, x; for(int i = 1; i <= n; i++) { scanf("%d", &num); for(j = 0; j < num; j++) { scanf("%d", &x); if(j == 0) map[i][x] = 0; else map[i][x] = 1; } } } void dijkstra() { int i, j; int next, Min; for(i = 1; i <= n; i++) { vis[i] = 0; dist[i] = map[s][i]; } vis[s] = 1; for(i = 2; i <= n; i++) { Min = INF; for(j = 1; j <= n; j++) { if(!vis[j] && Min > dist[j]) { Min = dist[j]; next = j; } } vis[next] = 1; for(j = 1; j <= n; j++) { if(!vis[j] && dist[next] + map[next][j] < dist[j]) dist[j] = dist[next] + map[next][j]; } } if(dist[e] == INF) printf("-1\n"); else printf("%d\n", dist[e]); } int main() { while(scanf("%d%d%d", &n, &s, &e) != EOF) { init(); getmap(); dijkstra(); } return 0; }
floyd:16ms
#include <cstdio> #include <cstring> #include <algorithm> #define INF 0x3f3f3f #define MAX 100+10 using namespace std; int n, s, e; int dist[MAX], map[MAX][MAX], vis[MAX]; void init() { for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(i == j) map[i][j] = 0; else map[i][j] = INF; } } } void getmap() { int num; int i, j, x; for(int i = 1; i <= n; i++) { scanf("%d", &num); for(j = 0; j < num; j++) { scanf("%d", &x); if(j == 0) map[i][x] = 0; else map[i][x] = 1; } } } void floyd() { int k, i, j; for(k = 1; k <= n; k++) { for(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) { map[i][j] = min(map[i][j], map[i][k]+map[k][j]); } } } if(map[s][e] == INF) printf("-1\n"); else printf("%d\n", map[s][e]); } int main() { while(scanf("%d%d%d", &n, &s, &e) != EOF) { init(); getmap(); floyd(); } return 0; }
spfa:32ms
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define INF 0x3f3f3f #define MAX 100+10 using namespace std; struct Edge { int to, val, next; }edge[1010]; int n, s, e; int dist[MAX], vis[MAX], top, head[MAX]; queue<int> Q; void init() { top = 0; for(int i = 1; i <= n; i++) { vis[i] = 0; head[i] = -1; } } void addedge(int a, int b, int d) { edge[top].to = b; edge[top].val = d; edge[top].next = head[a]; head[a] = top++; } void getmap() { int num; int i, j, x; for(i = 1; i <= n; i++) { scanf("%d", &num); for(j = 0; j < num; j++) { scanf("%d", &x); if(j == 0) addedge(i, x, 0); else addedge(i, x, 1); } } } void spfa() { int i; for(i = 1; i <= n; i++) dist[i] = INF; Q.push(s); vis[s] = 1;//标记 dist[s] = 0; while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = 0;//清除标记 for(i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if(dist[v] > dist[u] + edge[i].val) { dist[v] = dist[u] + edge[i].val; if(!vis[v]) { vis[v] = 1; Q.push(v); } } } } if(dist[e] == INF) printf("-1\n"); else printf("%d\n", dist[e]); } int main() { while(scanf("%d%d%d", &n, &s, &e) != EOF) { init(); getmap(); spfa(); } return 0; }