首页 > 代码库 > URAL 1930 Ivan's Car(BFS)

URAL 1930 Ivan's Car(BFS)

Ivan‘s Car

Time limit: 1.5 second
Memory limit: 64 MB
The world is in danger! Awful earthquakes are detected all over the world. Houses are destroyed, rivers overflow the banks, it is almost impossible to move from one city to another. Some roads are still useful, but even they became too steep because of soil movements.
Fortunately, engineer Ivan has a car, which can go well uphill and downhill. But there are different gear-modes for movement up and down, so during the driving you have to change gear-modes all the time. Also engineer Ivan has a good friend –– geologist Orlov. Together they are able to invent a plan for world saving. But, unfortunately, geologist Orlov lives in another town.
Ivan wants to save the world, but gear-box in his car started to wear out, so he doesn’t know, how long he will be able to use it. Please help Ivan to save the world. Find a route to the Orlov‘s town, such that Ivan will have to change gear-modes as few times as possible. In the beginning of the way Ivan can turn on any of gear-modes and you don‘t have to count this action as a changing of gear-mode.

Input

There are two positive integer numbers n and m in the first line, the number of towns and roads between them respectively (2 ≤ n ≤ 10 000; 1 ≤ m ≤ 100 000). Next m lines contain two numbers each — numbers of towns, which are connected by road. Moreover, the first is the town, which is situated below, from which you should go uphill by this road. Every road can be used for traveling in any of two directions. There is at most one road between any two cities. In the last line there are numbers of two cities, in which Ivan and geologist Orlov live, respectively. Although the majority of roads were destroyed, Ivan knows exactly, that the way to geologist Orlov‘s city exists.

Output

Output the smallest number of gear-modes changes on the way to Orlov‘s city.

Samples

inputoutput
3 21 23 21 3
1
3 31 22 33 11 3
0
Problem Author: Grigoriy Nazarov (prepared by Bulat Zaynullin)
【分析】题意就不说了,应该都能看懂,我说说我的做法,可能有点繁琐。
 先用vector存边,up存向上的,down存向下的,然后BFS,用优先队列存状态,每次都从队列中取操作数最小的,由于复杂度跟内存问题,需要减枝,当走到一个点,看当前的操作数跟上一次的大小,如果小则放入队列,如果相同则看这个点上次的方向与这一次是否相同,如果不相同且这个点的这个方向也没有出现过,则放入队列。
一开始一直错,改来改去到了Text 11,先是MLE,改了一下,WA,再改 TLE,都快放弃了,后来加了个vis,过了,以后遇到这种情况千万不能放弃。
技术分享
#include <iostream>#include <cstring>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <time.h>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#define inf 0x3f3f3f3f#define mod 1000000007typedef long long ll;using namespace std;const int N=10005;const int M=100005;vector<int>up[N],down[N];int n,m;int a,b;int step[N],lastdir[N];bool vis[N][3];struct man {    int sum;    int dir;    int num;    bool operator<(man aa)const {        return sum>aa.sum;    }};priority_queue<man>q;void init(){    for(int i=0; i<up[a].size(); i++) {        man s;        s.sum=0;        s.num=up[a][i];        s.dir=1;q.push(s);vis[a][1]=true;    }    for(int i=0; i<down[a].size(); i++) {        man s;        s.sum=0;        s.num=down[a][i];        s.dir=2;q.push(s);vis[a][2]=true;    }}void bfs() {    init();    memset(step,inf,sizeof(step));    step[a]=0;    while(!q.empty()){        man t=q.top();q.pop();        int tt=t.num;//printf("!!!dir=%d num=%d sum=%d\n",t.dir,t.num,t.sum);        if(tt==b){            printf("%d\n",t.sum);            return;        }        for(int i=0;i<up[tt].size();i++){            man k=t;k.num=up[tt][i];            if(t.dir==2)k.dir=1,k.sum++;            if(step[k.num]>k.sum){                step[k.num]=k.sum;lastdir[k.num]=k.dir;                q.push(k);            }            else if(step[k.num]==k.sum&&lastdir[k.num]!=k.dir&&!vis[k.num][k.dir]){                lastdir[k.num]=k.dir;                q.push(k);            }        }        for(int i=0;i<down[tt].size();i++){            man k=t;k.num=down[tt][i];            if(t.dir==1)k.dir=2,k.sum++;            if(step[k.num]>k.sum){                step[k.num]=k.sum;lastdir[k.num]=k.dir;                q.push(k);            }            else if(step[k.num]==k.sum&&lastdir[k.num]!=k.dir&&!vis[k.num][k.dir]){                lastdir[k.num]=k.dir;                q.push(k);            }        }    }}int main() {    memset(vis,false,sizeof(vis));    scanf("%d%d",&n,&m);    while(m--) {        scanf("%d%d",&a,&b);        up[a].push_back(b);        down[b].push_back(a);    }    scanf("%d%d",&a,&b);    bfs();    return 0;}
View Code

 

URAL 1930 Ivan's Car(BFS)