1930. Ivan's Car(spfa)
简单二维 标记一下是上坡还是下坡
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<queue> 8 using namespace std; 9 #define N 10010 10 #define INF 0xfffffff 11 int dis[N][2],vis[N]; 12 int n; 13 struct node 14 { 15 int u,v,next,d; 16 }ed[N*20]; 17 int t,head[N]; 18 void init() 19 { 20 t = 0; 21 memset(head,-1,sizeof(head)); 22 } 23 void add(int u,int v,int d) 24 { 25 ed[t].u = u; 26 ed[t].v = v; 27 ed[t].d = d; 28 ed[t].next = head[u]; 29 head[u] = t++; 30 } 31 int spfa(int s,int e) 32 { 33 int i; 34 for(i = 1; i <= n ;i++) 35 { 36 dis[i][0] = INF; 37 dis[i][1] = INF; 38 } 39 dis[s][0] = 0; 40 dis[s][1] = 0; 41 queue<int>q; 42 q.push(s); 43 vis[s] = 1; 44 while(!q.empty()) 45 { 46 int u = q.front(); 47 q.pop(); 48 for(i = head[u] ; i != -1 ; i = ed[i].next) 49 { 50 int v = ed[i].v; 51 int d = ed[i].d; 52 if(d) 53 dis[v][1] = min(min(dis[u][1],dis[u][0]+1),dis[v][1]); 54 else 55 dis[v][0] = min(min(dis[u][0],dis[u][1]+1),dis[v][0]); 56 if(!vis[v]) 57 { 58 vis[v] = 1; 59 q.push(v); 60 } 61 } 62 } 63 return min(dis[e][1],dis[e][0]); 64 } 65 int main() 66 { 67 int i,u,v,m; 68 init(); 69 scanf("%d%d",&n,&m); 70 for(i = 1; i <= m ;i++) 71 { 72 scanf("%d%d",&u,&v); 73 add(u,v,1); 74 add(v,u,0); 75 } 76 int s,e; 77 scanf("%d%d",&s,&e); 78 printf("%d\n",spfa(s,e)); 79 return 0; 80 }