HDU 2680Choose the best route (SPFA)
2014-05-03 20:21
323 查看
转载请注明出处:http://blog.csdn.net/u012860063
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680
注意:此题道路是单向的!
SPFA+反向寻找!
代码如下:
//有时正向不对,不防反向试试(生活亦如此)!
再附一份dijstra:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680
百度之星编程大赛——您报名了吗? 杭电ACM 2014暑期集训队——选拔安排~ |
Choose the best routeTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6285 Accepted Submission(s): 2067 Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n. Input There are several test cases. Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home. Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes . Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations. Output The output contains one line for each data set : the least time Kiki needs to spend ,if it’s impossible to find such a route ,just output “-1”. Sample Input 5 8 5 1 2 2 1 5 3 1 3 4 2 4 7 2 5 6 2 3 5 3 5 1 4 5 1 2 2 3 4 3 4 1 2 3 1 3 4 2 3 2 1 1 Sample Output 1 -1 Author dandelion Source 2009浙江大学计算机研考复试(机试部分)——全真模拟 Recommend lcy | We have carefully selected several similar problems for you: 2112 1217 2066 1142 1385 |
SPFA+反向寻找!
代码如下:
#include <cstdio> #include <cmath> #include <cstring> #include <cstdlib> #include <climits> #include <malloc.h> #include <ctype.h> #include <queue> #include <stack> #include <vector> #include <deque> #include <set> #include <map> #include <iostream> #include <algorithm> using namespace std; #define pi acos(-1.0) #define INF 0xffffff #define N 1047 #define M 20047 int n,m,s,pp,ww,k,Edgehead[M],dis[M]; struct { int v,w,next; }Edge[2*M]; bool vis[M]; void Addedge(int u,int v,int w) { Edge[k].next = Edgehead[u]; Edge[k].w = w; Edge[k].v = v; Edgehead[u] = k++; } void SPFA( int start) { queue<int>Q; for(int i = 1 ; i <= N ; i++ ) dis[i] = INF; dis[start] = 0; memset(vis,false,sizeof(vis)); Q.push(start); while(!Q.empty())//直到队列为空 { int u = Q.front(); Q.pop(); vis[u] = false; for(int i = Edgehead[u] ; i!=-1 ; i = Edge[i].next)//注意 { int v = Edge[i].v; int w = Edge[i].w; if(dis[v] > dis[u] + w) { dis[v] = dis[u]+w; if( !vis[v] )//防止出现环,也就是进队列重复了 { Q.push(v); vis[v] = true; } } } } } int main() { int u,v,w; while(~scanf("%d%d%d",&n,&m,&s)) { k = 1; memset(Edgehead,-1,sizeof(Edgehead)); for(int i = 1 ; i <= m ; i++ ) { scanf("%d%d%d",&u,&v,&w); // Addedge(u,v,w); Addedge(v,u,w);//因为是反向寻找的而且道路是单向的,所以此处也要方向 } SPFA(s); scanf("%d",&pp); int min = INF; for(i = 0 ; i < pp ;i++) { scanf("%d",&ww); if(min > dis[ww]) min = dis[ww]; } if(min == INF) min = -1; printf("%d\n",min); } return 0; }
//有时正向不对,不防反向试试(生活亦如此)!
再附一份dijstra:
#include<stdio.h> #define max 1001 #define INF 100000000 int N; int v[max]; int dis[max]; int g[max][max]; void dijk() { int i,j,mark,mindis; for(i=0;i<=N;i++) { v[i] = 0; dis[i] = INF; } dis[0] = 0; v[0] = 1; for(i=0;i<=N;i++) { mindis = INF; mark = 0; for(j=0;j<=N;j++) if(!v[j] && dis[j] < mindis) { mindis = dis[j]; mark = j; } v[mark] = j; for(j=0;j<=N;j++) if(!v[j] && dis[j] > dis[mark] + g[mark][j]) dis[j] = dis[mark] + g[mark][j]; } } int main() { int W,M,Q,i,j,d,a,b; while(~scanf("%d%d%d",&N,&M,&Q)) { for(i=0;i<max;i++) for(j=0;j<max;j++) { if(i==j)g[i][j] = 0; else g[i][j] = INF; } while(M--) { scanf("%d%d%d",&a,&b,&d); if(d < g[a][b]) g[a][b] = d; } scanf("%d",&W); while(W--) { scanf("%d",&a); g[0][a] = 0; } dijk(); if(dis[Q] == INF) printf("-1\n"); else printf("%d\n",dis[Q]); } }
相关文章推荐
- HDU-2680-Choose the best route【spfa】
- HDU 2680 Choose the best route【多源最短路,Dijkstra+spfa】
- HDU-2680 Choose the best route(SPFA)
- hdu 2680 Choose the best route(spfa/dijkstra)
- hdu 2680 Choose the best route (最短路问题 dijkstra | spfa)
- HDU 2680 Choose the best route (SPFA 算法求最短路径o(ke))
- HDU 2680 Choose the best route 【最短路 反向建图 dijkstra & SPFA 】
- hdu 2680 Choose the best route(SPFA或者Dijkstra)
- HDU 2680 Choose the best route
- HDU-2680 Choose the best route
- hdu2680 Choose the best route 最短路,超级起点(多源Dijkstra算法,)
- HDU 2680 Choose the best route (最短路+反向图)
- hdu 2680 Choose the best route【dijstra+反向建图】
- hdu 2680 Choose the best route (dijkstra算法)
- hdu 2680 Choose the best route
- HDU 2680 Choose the best route
- HDU 2680 Choose the best route
- HDU--2680Choose the best route【Dijkstra】
- HDU 2680-Choose the best route
- hdu 2680 Choose the best route