Two kids are going to travel by plane. Now, I want to give you the opportunity to take a plane free of charge and ask what the minimum charge is from the start point to the end point.
Idea: the first question of the layered graph. When I heard the layered graph, I thought it was really a K-graph creation, and then the vertices and edges between different layers ran the shortest path .. Later I found out that I thought too much ..
In fact, it is still the thought of "dynamic". (is the most short circuit the thought of "dynamic? ω? '), F [I] [J] indicates the minimum cost when the I free opportunity is used at the J position, then the one-dimensional transfer in spfa is enough.
PS: This question on bzoj is still quite a constant. I used queue to use 9388ms, which is almost a little more dangerous. I switched to priority_queue, and the time suddenly reached 276 Ms .. -The power of O2 ..
Code:
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 100000#define INF 0x7f7f7f7fusing namespace std;int f[11][MAX];struct Complex{int pos,step;Complex(int _step,int _pos):pos(_pos),step(_step) {}Complex() {}bool operator <(const Complex &a)const {return f[step][pos] > f[a.step][a.pos];}}; int points,edges,steps; int start,end;bool v[11][MAX];int head[MAX],total;int next[MAX],aim[MAX],length[MAX]; inline void Add(int x,int y,int len);void SPFA(); int main(){ cin >> points >> edges >> steps; cin >> start >> end; start++,end++; for(int x,y,z,i = 1;i <= edges; ++i) { scanf("%d%d%d",&x,&y,&z); x++,y++; Add(x,y,z),Add(y,x,z); } SPFA(); int ans = INF; for(int i = 0;i <= steps; ++i) ans = min(ans,f[i][end]); cout << ans; return 0;} inline void Add(int x,int y,int len){ next[++total] = head[x]; aim[total] = y; length[total] = len; head[x] = total;} void SPFA(){ static priority_queue<Complex> q; memset(f,0x3f,sizeof(f)); q.push(Complex(0,start)); f[0][start] = 0; while(!q.empty()) { Complex temp = q.top(); q.pop(); int x = temp.pos,step = temp.step; v[step][x] = false; for(int i = head[x];i;i = next[i]) { if(f[step][aim[i]] > f[step][x] + length[i]) { f[step][aim[i]] = f[step][x] + length[i]; if(!v[step][aim[i]]) { v[step][aim[i]] = true; q.push(Complex(step,aim[i])); } } if(f[step + 1][aim[i]] > f[step][x] && step < steps) { f[step + 1][aim[i]] = f[step][x]; if(!v[step + 1][aim[i]]) { v[step + 1][aim[i]] = true; q.push(Complex(step + 1,aim[i])); } } } }}
Bzoj 2763 jloi 2011 flight route layering + Shortest Path