HDU 2680 Choose the best route
2014-09-12 10:25
260 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2680
裸的有向图最短路问题 使用SPFA + 邻接表 有一个小地方是需要将图反向。
数组实现邻接表:
SPFA:
主函数:
http://acm.hdu.edu.cn/showproblem.php?pid=2680
裸的有向图最短路问题 使用SPFA + 邻接表 有一个小地方是需要将图反向。
数组实现邻接表:
struct edge{ int to; int cost; int next; }adj[20010]; int head[1010],num; void addedge(int from, int to, int cost){ adj[num].to = to; adj[num].cost = cost; adj[num].next = head[from]; head[from] = num++; }
SPFA:
int dis[1010],vis[1010],enter[1010]; bool SPFA(int scr){ for(int i=1; i<=V; i++){ dis[i] = 1e9; vis[i] = 0; enter[i] = 0; } vis[scr] = 1; dis[scr] = 0; enter[scr] = 1; queue <int> Q; Q.push(scr); while(!Q.empty()){ int tmp = Q.front(); Q.pop(); vis[tmp] = 0; for(int i=head[tmp]; i!=-1; i=adj[i].next){ if(dis[adj[i].to] > dis[tmp] + adj[i].cost){ dis[adj[i].to] = dis[tmp] + adj[i].cost; if(!vis[adj[i].to]){ vis[adj[i].to] = 1; enter[adj[i].to]++; if(enter[adj[i].to] >= V) return false; Q.push(adj[i].to); } } } } return true; }
主函数:
int main(){ // freopen("in.txt", "r", stdin); while(scanf("%d%d%d",&V,&E,&key) == 3){ memset(head, -1, sizeof(head)); num = 0; for(int i=0; i<E; i++){ int from, to, cost; scanf("%d%d%d",&from,&to,&cost); addedge(to, from, cost); } scanf("%d",&goodcnt); for(int i=0; i<goodcnt; i++) scanf("%d",&id[i]); int ans = 1e9; SPFA(key); for(int i=0; i<goodcnt; i++) ans = min(ans, dis[id[i]]); if(ans == 1e9) ans = -1; printf("%d\n",ans); } return 0; }
相关文章推荐
- 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 2680 Choose the best route
- (step6.2.4)hdu 2680(Choose the best route——最短路径)
- HDU 2680 Choose the best route(dijkstra)
- 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-2680 choose the best route
- hdu 2680 Choose the best route
- hdu 2680 Choose the best route
- HDU 2680 Choose the best route(单源最短路径)
- hdu 2680 Choose the best route
- hdu 2680 Choose the best route
- hdu 2680 Choose the best route
- HDU 2680-Choose the best route
- 【最短路径-Dis】hdu 2680 Choose the best route