题目1162:I Wanna Go Home(最短路径问题进阶dijkstra算法))
题目链接:http://ac.jobdu.com/problem.php?pid=1162
详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus
参考代码:
// // 1162 I Wanna Go Home.cpp // Jobdu // // Created by PengFei_Zheng on 20/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <cmath> #define MAX 0x7fffffff using namespace std; int n ,m; int grah[601][601]; int sup[601]; void dijkstra() { int dis[601]; bool mark[601] = {false}; for(int i = 0 ; i < n; i++) dis[i] = grah[0][i]; int s = 0; mark[s] = true; dis[s] = 0; int newP = s; for(int i = 1 ; i <= n ; i++){ for( int j = 0 ; j < n ; j++){ if( !mark[j] && grah[newP][j] < MAX && !(sup[newP] == 2 && sup[j] == 1)){ if(dis[j] > dis[newP] + grah[newP][j]) dis[j] = dis[newP] + grah[newP][j]; } } int min = MAX; for(int j = 0 ; j < n ; j++){ if( !mark[j] && min > dis[j]){ min = dis[j]; newP = j; } } mark[newP] = true; } if(dis[1] < MAX) printf("%d\n",dis[1]); else printf("-1\n"); } int main(){ while(scanf("%d",&n)!=EOF && n!=0){ for(int i = 0 ; i < n ; i++){ for(int j = 0 ; j < n ; j++){ if(i==j) { grah[i][j]=0; } else grah[i][j]=MAX; } } scanf("%d",&m); int x, y, len; while(m--){ scanf("%d%d%d",&x,&y,&len); grah[x-1][y-1] = grah[y-1][x-1]=len; } for(int i = 0 ; i < n ; i++){ scanf("%d",&sup[i]); } dijkstra(); } return 0; } /************************************************************** Problem: 1162 User: zpfbuaa Language: C++ Result: Accepted Time:10 ms Memory:2932 kb ****************************************************************/
作者: 伊甸一点
出处: http://www.cnblogs.com/zpfbuaa/
本文版权归作者伊甸一点所有,欢迎转载和商用(须保留此段声明),且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文链接 如有问题, 可邮件(zpflyfe@163.com)咨询.