标签:hdu2962 trucking 最短路 spfa dijkstra
5 6 1 2 7 5 1 3 4 2 2 4 -1 10 2 5 2 4 3 4 10 1 4 5 8 5 1 5 10 5 6 1 2 7 5 1 3 4 2 2 4 -1 10 2 5 2 4 3 4 10 1 4 5 8 5 1 5 4 3 1 1 2 -1 100 1 3 10 0 0
Case 1: maximum height = 7 length of shortest route = 20 Case 2: maximum height = 4 length of shortest route = 8 Case 3: cannot reach destination
#include <stdio.h> #include <string.h> #define INF 1000000000 #define MAX 1010 int dis[MAX] ; struct Graph{ int len , height ; }graph[MAX][MAX]; bool visited[MAX] ; int s , d ; int dijkstra(int n , int limit) { for(int i = 1 ; i <= n ; ++i) { if(graph[s][i].height<limit) { dis[i] = INF ; } else { dis[i] = graph[s][i].len ; } visited[i] = false ; } dis[s] = 0 ; visited[s] = true ; for(int i = 1 ; i < n ; ++i) { int index = -1 , min = INF ; for(int j = 1 ; j <= n ; ++j) { if(!visited[j] && min>dis[j]) { min = dis[j] ; index = j ; } } if(index == -1) { return dis[d]; } visited[index] = true ; for(int j = 1 ; j <= n ; ++j) { if(graph[index][j].height<limit) { continue ; } if(!visited[j] && dis[j]>dis[index]+graph[index][j].len) { dis[j] = dis[index]+graph[index][j].len ; } } } return dis[d] ; } int main() { int c , r , cs = 1; while(~scanf("%d%d",&c,&r)&&(c+r)) { if(cs != 1) puts("") ; for(int i = 0 ; i <= c ; ++i) { for(int j = 0 ; j <= i ; ++j) { graph[i][j].len = graph[j][i].len = INF ; } } for(int i = 0 ; i < r ; ++i) { int x , y , h , l ; scanf("%d%d%d%d",&x,&y,&h,&l) ; if( h == -1) h = INF ; graph[x][y].len = graph[y][x].len = l ; graph[x][y].height = graph[y][x].height = h ; } int limit ; scanf("%d%d%d",&s,&d,&limit) ; int i = 1 , mid=1 , j =limit , ans = INF ; while(i<=j) { mid = (i+j)/2 ; int temp = dijkstra(c,mid) ; if(temp != INF) { ans = temp ; i = mid + 1 ; } else j = mid - 1 ; } printf("Case %d:\n",cs++); if(ans != INF) { printf("maximum height = %d\n",j); printf("length of shortest route = %d\n",ans); } else printf("cannot reach destination\n"); } return 0 ; }
#include <cstdio> #include <cstring> #include <deque> #define INF 1000000000 #define MAX 1010 using namespace std ; int dis[MAX] ; struct Graph{ int len , height ; }graph[MAX][MAX]; bool visited[MAX] ; int s , d ; int spfa(int n , int limit) { int c[MAX] ; for(int i = 1 ; i <= n ; ++i) { dis[i] = INF ; c[i] = 0 ; visited[i] = false ; } dis[s] = 0 ; visited[s] = true ; deque<int> que ; que.push_back(s) ; while(!que.empty()) { int k = que.front() ; visited[k] = false ; que.pop_front() ; c[k]++ ; if(c[k]>n) { break ; } for(int i = 1 ; i <= n ; ++i) { if(graph[k][i].height<limit) continue ; if(dis[i]>dis[k]+graph[k][i].len) { dis[i] = dis[k]+graph[k][i].len ; if(que.empty()) que.push_back(i) ; else { if(dis[i]>dis[que.front()]) que.push_back(i) ; else que.push_front(i) ; } visited[i] = true ; } } } return dis[d] ; } int main() { int c , r , cs = 1; while(~scanf("%d%d",&c,&r)&&(c+r)) { if(cs != 1) puts("") ; for(int i = 0 ; i <= c ; ++i) { for(int j = 0 ; j <= i ; ++j) { graph[i][j].len = graph[j][i].len = INF ; } } for(int i = 0 ; i < r ; ++i) { int x , y , h , l ; scanf("%d%d%d%d",&x,&y,&h,&l) ; if( h == -1) h = INF ; graph[x][y].len = graph[y][x].len = l ; graph[x][y].height = graph[y][x].height = h ; } int limit ; scanf("%d%d%d",&s,&d,&limit) ; int i = 1 , mid=1 , j =limit , ans = INF ; while(i<=j) { mid = (i+j)/2 ; int temp = spfa(c,mid) ; if(temp != INF) { ans = temp ; i = mid + 1 ; } else j = mid - 1 ; } printf("Case %d:\n",cs++); if(ans != INF) { printf("maximum height = %d\n",j); printf("length of shortest route = %d\n",ans); } else printf("cannot reach destination\n"); } return 0 ; }
hdu 2962 Trucking 最短路+二分。。Dijkstra+SPFA两种算法实现。
标签:hdu2962 trucking 最短路 spfa dijkstra
原文地址:http://blog.csdn.net/lionel_d/article/details/44874443