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

input output
3 2
1 2
3 2
1 3
1
3 3
1 2
2 3
3 1
1 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 1000000007
typedef 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;
}

URAL 1930 Ivan's Car(BFS)

时间: 2024-12-28 21:38:26

URAL 1930 Ivan's Car(BFS)的相关文章

pots(BFS)

D - Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: Input On the first and

USACO抓牛catchcow (bfs)

这题是黄巨大出的比赛题. http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤

Ural 1260 A nudnik photographer(DP)

A nudnik photographer 大意: 对1到N这些数进行排列,1必须要在最左边,相邻的两个数之间的差值不能超过2,问有多少种排列的方法. 思路: 对座位进行DP,当第一个是1,第二个是2的时候,组合为dp[i-1]:当第一个是1,第二个是3的时候,第三个也确定了是2,组合为dp[i-3]:还有最后一种情况是1357--8642. 所以DP方程为dp[i] = dp[i-1]+dp[i-3]+1. #include <stdio.h> int n; int dp[100]; int

hdu 1728 逃离迷宫 (BFS)

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14376    Accepted Submission(s): 3458 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方

牛汇(BFS)入金具体流程(图文指导)

牛汇开户流程:bfsforex牛汇入金教程 所谓入金,也就是充值的意思,必须充钱到平台才能进行外汇交易.首先,我们先登录bfsforex牛汇官方网站,在交易办公室功能区域下面,点击账户入金: 为您提供中国各大银行的网银支付解决方案,支持人民币支付,和信用卡入金,入金是实时到账的. 牛汇(BFS)入金具体流程(图文指导),布布扣,bubuko.com

【判重+广搜(bfs)】魔板

判重+广搜(bfs)]魔板 Time Limit: 1000MS Memory Limit: 32768KB Special Judge 有一个两行四列的魔板,每个格子里有一个1到8的数字(数字唯一),现在我们可以对魔板进行以下操作: 1.交换两行的数字. 2.将第一列移到第二列,第二列到第三列,第三列到第四列,第四列到第一列. 3.将中间四个数顺时针转一次. 现给你初始状态,我末状态请你用最小的步数将它从初始状态变到末状态. 输入: 前两行,每行4个数表示初状态. 后两行,每行4个数表示末状态

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

USACO Mother&amp;#39;s Milk(bfs)

a=9MvljJDNdls&S=milk3">题目请点我 题解: 水杯倒水的问题非常经典,套路也是一样的,bfs找出全部状态. 这道题的关键在于每次都应该进行六次的倒水尝试,细心一点.PS:三维数组表示状态真的非常方便. 代码实现: /* ID: eashion LANG: C++ TASK: milk3 */ #include <iostream> #include <cstdio> #include <cstdlib> #include &l

[C++]广度优先搜索(BFS)(附例题)

广度优先搜索(BFS)(附例题) 问题产生: Isenbaev是国外的一个大牛. 现在有许多人要参加ACM ICPC. 一共有n个组,每组3个人.同组的3个人都是队友. 大家都想知道自己与大牛的最小距离是多少. 大牛与自己的最小距离当然是0.大牛的队友和大牛的最小距离是1.大牛的队友的队友和大牛的最小距离是2--以此类推. 如果实在和大牛没有关系的只好输出undefined了. 第一行读入n.表示有n个组.1 ≤ n ≤ 100 接下来n行,每行有3个名字,名字之间用空格隔开.每个名字的开头都是