标签:des style blog http io color ar os for
Description
Input
Output
Sample Input
1 6 2 1 2 10 2 3 10 3 4 10 4 5 10 5 6 10 6 1 10
Sample Output
5
这是仿照人家写的SPFA,然后补上其他3个最短路算法
1 /好长时间没写过最短路了,好多基础的东西都不记得了 c 2 #include <cstdio> 3 #include <iostream> 4 #include <cstring> 5 #include <cmath> 6 #include <algorithm> 7 #include <queue> 8 9 using namespace std; 10 11 12 const int N = 5050, INF = 10000; 13 14 int head[N], nc, n; /// 15 int dis[N]; ///计算距离的数组 16 int stk[N], f, r; ///用数组模拟栈, 17 bool vis[N]; ///在用spfa时的标记数组 18 19 struct Edge{ ///边 20 int to, next, cost; 21 }edge[100000]; 22 23 void add(int a, int b, int c) ///加边函数,双向 24 { 25 edge[nc].to = b; 26 edge[nc].next = head[a]; 27 edge[nc].cost = c; 28 head[a] = nc++; 29 30 edge[nc].to = a; 31 edge[nc].next = head[b]; 32 edge[nc].cost = c; 33 head[b] = nc++; 34 } 35 36 void fire_spfa(int fire[], int num) 37 { 38 memset(vis, false, sizeof(vis)); 39 40 f = r = 0; ///初始化栈 41 42 for(int i = 0; i < num; i++) ///初始化每个消防站到其他点的距离 43 { 44 int t = fire[i]; 45 if(!vis[t]) 46 { 47 vis[t] = true; 48 dis[stk[r++] = t] = 0;///初始化,并将当前点入栈 49 } 50 } 51 52 while(f != r) ///如果栈不为空 53 { 54 int now = stk[f++]; ///从栈中取出栈顶元素 55 if(f == N) f = 0; ///防止栈空间不够 56 57 vis[now] = false; ///将当前结点标记 58 for(int i = head[now]; i != -1; i = edge[i].next) ///从边表中取出元素 59 { 60 int to = edge[i].to; 61 int cot = edge[i].cost; 62 63 if(dis[to] > dis[now] + cot) ///进行松弛操作 64 { 65 dis[to] = dis[now] + cot; 66 if(!vis[to]) ///如果前驱没有访问过, 67 { 68 stk[r++] = to; ///入栈 69 if(r == N) 70 r = 0; 71 vis[to] = true; ///标记 72 } 73 } 74 } 75 } 76 } 77 78 int spfa(int src) 79 { 80 memset(vis, false, sizeof(vis)); 81 f = 0; 82 r = 1; 83 84 vis[src] = true; 85 int d[N]; 86 for(int i = 1; i <= n; ++i) ///从当前结点到其余所有结点距离初始化 87 { 88 d[i] = INF; 89 } 90 91 d[src] = 0; 92 stk[0] = src; 93 94 while(f != r) 95 { 96 int now = stk[f++]; 97 if(f == N) 98 f = 0; 99 vis[now] = false; 100 101 for(int i = head[now]; i != -1; i = edge[i].next) 102 { 103 int to = edge[i]. to; 104 int cot = edge[i].cost; 105 106 if(d[to] > d[now] + cot) 107 { 108 d[to] = d[now] + cot; 109 if(!vis[to]) 110 { 111 stk[r++] = to; 112 vis[to] = true; 113 if(r == N) 114 r = 0; 115 } 116 } 117 } 118 } 119 120 int ans = 0; 121 for(int i = 1; i <= n; ++i) 122 { 123 ans = max(ans, min(d[i], dis[i])); 124 } 125 return ans; 126 } 127 128 int main() 129 { 130 int ff, fire[N], a, b, c; 131 132 memset(head, -1, sizeof(head)); 133 nc = 0; 134 scanf("%d%d", &ff, &n); 135 for(int i = 0; i < ff; ++i) 136 { 137 scanf("%d", fire+i); 138 } 139 140 while(scanf("%d%d%d", &a, &b, &c) != EOF) 141 { 142 add(a, b, c); 143 } 144 145 for(int i = 1; i <= n; ++i) 146 dis[i] = INF; 147 148 fire_spfa(fire, ff); 149 150 int id = 1, ans = INF; 151 152 for(int i = 1; i <= n; ++i) 153 { 154 if(dis[i] != 0) 155 { 156 int tp = spfa(i); 157 if(ans > tp) 158 { 159 ans = tp; 160 id = i; 161 } 162 } 163 } 164 165 printf("%d\n", id); 166 return 0; 167 }
标签:des style blog http io color ar os for
原文地址:http://www.cnblogs.com/ya-cpp/p/4075240.html