zoukankan      html  css  js  c++  java
  • Day4

    Tram network in Zagreb consists of a number of intersections and rails connecting some of them. In every intersection there is a switch pointing to the one of the rails going out of the intersection. When the tram enters the intersection it can leave only in the direction the switch is pointing. If the driver wants to go some other way, he/she has to manually change the switch. 

    When a driver has do drive from intersection A to the intersection B he/she tries to choose the route that will minimize the number of times he/she will have to change the switches manually. 

    Write a program that will calculate the minimal number of switch changes necessary to travel from intersection A to intersection B. 

    Input

    The first line of the input contains integers N, A and B, separated by a single blank character, 2 <= N <= 100, 1 <= A, B <= N, N is the number of intersections in the network, and intersections are numbered from 1 to N. 

    Each of the following N lines contain a sequence of integers separated by a single blank character. First number in the i-th line, Ki (0 <= Ki <= N-1), represents the number of rails going out of the i-th intersection. Next Ki numbers represents the intersections directly connected to the i-th intersection.Switch in the i-th intersection is initially pointing in the direction of the first intersection listed. 

    Output

    The first and only line of the output should contain the target minimal number. If there is no route from A to B the line should contain the integer "-1".

    Sample Input

    3 2 1
    2 2 3
    2 3 1
    2 1 2
    

    Sample Output

    0
    简述:题有点难懂,给你N组数以及起点A终点B,节点标号1-N,接下来每一行第一个数表示i-th连接有几个节点,后面的第一个数是默认方向不用改变,后续的都是需要改变一次方向。
    思路:看懂题意后就是一个最短路问题,默认方向权为0,改变为1,四种算法选一种即可,我这里用的是dijkstra,(其他三种在A题中有,这里就不写了),代码如下:
    const int maxm = 110;
    const int INF = 0x7ffffff;
    
    int N, A, B, d[maxm], vis[maxm];
    
    struct Edge {
        int from, to, dist;
        Edge(int _from, int _to, int _dist) : from(_from), to(_to), dist(_dist){};
    };
    
    struct Node {
        int from, dist;
        Node(int _from, int _dist) : from(_from), dist(_dist){}
        bool operator<(const Node &a)const {
            return a.dist < dist;
        }
    };
    
    vector<Edge> edges;
    vector<int> G[maxm];
    
    void addedge(int u, int v, int dist) {
        edges.push_back(Edge(u, v, dist));
        G[u].push_back(edges.size() - 1);
    }
    
    void init() {
        for(int i = 1; i <= N; ++i) {
            d[i] = INF;
            G[i].clear();
        }
        edges.clear();
        memset(vis, 0, sizeof(vis));
    }
    
    int main() {
        while(scanf("%d%d%d", &N, &A, &B) != EOF) {
            init();
            for (int i = 1; i <= N; ++i) {
                int t1, t2;
                scanf("%d", &t1);
                for(int j = 0; j < t1; ++j) {
                    scanf("%d", &t2);
                    addedge(i, t2, j == 0 ? 0 : 1);
                }
            }
            priority_queue<Node> q;
            q.push(Node(A, 0));
            d[A] = 0;
            while(!q.empty()) {
                Node p = q.top();
                q.pop();
                if(vis[p.from])
                    continue;
                vis[p.from] = 1;
                int len = G[p.from].size();
                for(int i = 0; i < len; ++i) {
                    if(d[edges[G[p.from][i]].to] > d[p.from] + edges[G[p.from][i]].dist) {
                        d[edges[G[p.from][i]].to] = d[p.from] + edges[G[p.from][i]].dist;
                        q.push(Node(edges[G[p.from][i]].to, d[edges[G[p.from][i]].to]));
                    }
                }
            }
            printf("%d
    ", d[B] >= INF?-1:d[B]);
        }
        return 0;
    }
    View Code

  • 相关阅读:
    665. Non-decreasing Array
    35. Search Insert Position
    CompositePattern(组合模式)-----Java/.Net
    BridgePattern(桥接模式)-----Java/.Net
    AdapterPattern(适配器模式)-----Java/.Net
    设计模式系列:原型模式(Prototype Pattern)
    设计模式系列:抽象工厂模式(Abstract Factory Pattern)
    设计模式系列:工厂方法模式(Factory Method Pattern)
    设计模式系列:简单工厂模式(Simple Factory Pattern)
    设计模式系列:单例模式(Singleton Pattern)
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/11287764.html
Copyright © 2011-2022 走看看