为了将最小费用最大流的spfa优化,决定将spfa换成heap优化的Dijkstra。
所以还得现学。。。
stl大法好。
——附带码
1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 #define Heap pair<int, int> 5 6 using namespace std; 7 8 const int INF = 2147483647; 9 int n, m, t, cnt; 10 int next[1000001], to[1000001], val[1000001], head[10001], dis[10001]; 11 priority_queue <Heap, vector <Heap>, greater <Heap> > q; 12 13 14 inline void add(int a, int b, int c) 15 { 16 to[cnt] = b; 17 val[cnt] = c; 18 next[cnt] = head[a]; 19 head[a] = cnt++; 20 } 21 22 inline void Dijkstra(int s) 23 { 24 int i, v; 25 Heap u; 26 for(i = 1; i <= n; i++) dis[i] = INF; 27 dis[s] = 0; 28 q.push(make_pair(0, s)); 29 while(!q.empty()) 30 { 31 u = q.top(); 32 q.pop(); 33 if(u.first != dis[u.second]) continue; 34 for(i = head[u.second]; i != -1; i = next[i]) 35 { 36 v = to[i]; 37 if(dis[v] > dis[u.second] + val[i]) 38 { 39 dis[v] = dis[u.second] + val[i]; 40 q.push(make_pair(dis[v], v)); 41 } 42 } 43 } 44 } 45 46 int main() 47 { 48 int i, j, a, b, c, s; 49 scanf("%d %d %d", &n, &m, &s); 50 memset(head, -1, sizeof(head)); 51 for(i = 1; i <= m; i++) 52 { 53 scanf("%d %d %d", &a, &b, &c); 54 add(a, b, c); 55 } 56 Dijkstra(s); 57 for(i = 1; i <= n; i++) printf("%d ", dis[i]); 58 return 0; 59 }
时间: 2024-12-22 01:10:19