WOJ 1006-Language of Animals解题心得

作为Java选手感觉受到了歧视。。。一样的代码逻辑java就疯狂超时,c++瞬间秒杀😅
在这里插入图片描述
c++真香
在这里插入图片描述
这件事情告诉我们,要用c++刷题,要讲武德,不能搞聪明,小聪明啊。

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int bfs(const vector<vector<int>>& adjGraph, int begin, int end){
    int n = adjGraph.size();
    vector<int> dist(n, 0x7fffffff);
    vector<bool> visited(n, false);
    int temp;
    queue<int> que;

    que.push(begin);
    dist[begin] = 0;
    visited[begin] = true;

    while (!que.empty()) {
        temp = que.front(); que.pop();
        for (int node : adjGraph[temp]) {
//            if (dist[node] > dist[temp] + 1 && !visited[node]) {  // 不需要松弛判断,因为边权值都是1
            if(!visited[node]) {
                dist[node] = dist[temp] + 1;
                visited[node] = true;
                que.push(node);
                if (node == end) {
                    return dist[node];
                }
            }
        }
    }
    return dist[end];
}

int main(){
    int n, m, s, e;
    cin >> n >> m;
    vector<vector<int>> adjGraph(n);
    for (int i = 0; i < m; ++i) {
        cin >> s >> e;
        adjGraph[s].push_back(e);
        adjGraph[e].push_back(s);
    }
    int k;
    cin >> k;
    for (int i = 0; i < k; ++i) {
        cin >> s >> e;
        if (s == e) {
            cout << 0 << endl;
        } else {
            int res = bfs(adjGraph, s, e);
            if (res == 0x7fffffff) {
                cout << -1 << endl;
            } else {
                cout << res - 1 << endl;
            }
        }
    }
    return 0;
}

下面是气急败坏java写的各种图最短路径算法,分享出来供大家学习(test3 超时):

public class LanguageOfAnimal {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] nums = reader.readLine().split(" ");
        int n = Integer.parseInt(nums[0]), m = Integer.parseInt(nums[1]);
        List<List<Integer>> adjGraph = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            adjGraph.add(new LinkedList<>());
        }
        for (int i = 0; i < m; i++) {
            nums = reader.readLine().split(" ");
            int from = Integer.parseInt(nums[0]), to = Integer.parseInt(nums[1]);
            adjGraph.get(from).add(to);
            adjGraph.get(to).add(from);
        }

        nums = reader.readLine().split(" ");
        int k = Integer.parseInt(nums[0]);
        for (int i = 0; i < k; i++) {
            nums = reader.readLine().split(" ");
            int a = Integer.parseInt(nums[0]), b = Integer.parseInt(nums[1]);
//            System.out.println("begin = " + a + " end = " + b);
            if (a == b) {
                System.out.println(0);
            } else {
                int res = spf(adjGraph, a, b, n);
                System.out.println(res == Integer.MAX_VALUE ? -1 : res-1);
            }
        }
    }

    public static int spf(List<List<Integer>> graph, int start, int end, int n) {
        int[] dist = new int[n];
        Arrays.fill(dist, Integer.MAX_VALUE);
        boolean[] visited = new boolean[n];
        int tempNode;
        List<Integer> edges;
        Queue<Integer> queue = new LinkedList<>();

        queue.add(start);
        visited[start] = true;
        dist[start] = 0;

        while (!queue.isEmpty()) {
            tempNode = queue.poll();
            edges = graph.get(tempNode);
            for(Integer nextNode : edges) {
                if (!visited[nextNode] && dist[tempNode] + 1 < dist[nextNode]) {
                    queue.add(nextNode);
                    dist[nextNode] = dist[tempNode] + 1;
                    visited[nextNode] = true;
                    if (nextNode == end) {
                        return dist[nextNode];
                    }
                }
            }
        }
        return dist[end];
    }

    public static int minLen;
    // 用邻接矩阵
    public static void matgraph(){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt(), m = scanner.nextInt();
        int[][] graph = new int[n][n];
        for (int i = 0; i < m; i++) {
            int from = scanner.nextInt(), to = scanner.nextInt();
            graph[from][to] = 1;
            graph[to][from] = 1;
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j) {
                    graph[i][j] = 0;
                } else if (graph[i][j] != 1) {
                    graph[i][j] = Integer.MAX_VALUE;
                }
            }
        }

        // floyd timeout!

        int k = scanner.nextInt();
        int[] dist = new int[n];
        for (int i = 0; i < k; i++) {
            int a = scanner.nextInt(), b = scanner.nextInt();
            if (a == b || graph[a][b] != Integer.MAX_VALUE) {
                System.out.println(0);
                continue;
            }
            Arrays.fill(dist, Integer.MAX_VALUE);
            bfs(graph, a, dist);
            // floyd
//                System.out.println(-1);
            // dfs
//                minLen = Integer.MAX_VALUE;
//                boolean[] visited = new boolean[n];
//                dfs(graph, a,visited, b, Integer.MAX_VALUE);
//                System.out.println(minLen == Integer.MAX_VALUE ? -1 : minLen-1);
//                graph[a][b] = minLen;
            if (dist[b] == Integer.MAX_VALUE){
                System.out.println(-1);
            } else {
                System.out.println(dist[b]-1);
            }
        }
    }

    public static void bfs(int[][]graph, int start, int[] dist) {
        boolean[] visited = new boolean[graph.length];
        Queue<Integer> queue = new LinkedList<>();

        queue.add(start);
        dist[start] = 0;
        visited[start] = true;

        while (!queue.isEmpty()) {
            int node = queue.poll();
            for (int i = 0; i < graph.length; i++) {
                if (!visited[i] && graph[node][i] != Integer.MAX_VALUE) {
//                    if (dist[i] > dist[node] + 1) {
                    dist[i] = dist[node] + 1;
//                    }
                    queue.add(i);
                    visited[i] = true;
                }
            }
        }
    }

    // Timeout!
    public static void dfs(int[][] graph, int curNode, boolean[] visited, int target, int pathlen) {
        if (curNode != target) {
            visited[curNode] = true;
            for (int i = 0; i < graph.length; i++) {
                if (!visited[i] && graph[curNode][i] != Integer.MAX_VALUE) {
                    if (pathlen == Integer.MAX_VALUE) {
                        pathlen = 0;
                    }
                    dfs(graph, i, visited, target, pathlen+1);
                }
            }
            visited[curNode] = false;
        } else {
            if (pathlen < minLen) {
                minLen = pathlen;
            }
        }
    }

    public static void floyd(int[][] graph){
        for(int k = 0; k < graph.length; k++) {
            for (int i = 0; i < graph.length; i++) {
                for (int j = 0; j < graph.length; j++) {
                    if (graph[i][k] == Integer.MAX_VALUE || graph[k][j] == Integer.MAX_VALUE) {
                        continue;
                    }
                    if (graph[i][j] > graph[i][k] + graph[k][j]) {
                        graph[i][j] = graph[i][k] + graph[k][j];
                    }
                }
            }
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值