2.1

2.1

P1144 最短路计数 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

  • 用java会吃T,java底层没有加速这道题会被卡常数
  • 本质上就是用bfs来进行搜索每个点与点1的最近距离,然后维护两个数组来计算路径条数,其实就是一个spfa算法的实现
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.*;

public class Main implements Runnable {

    static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
    static int n, m;
    static final int N = (int) (1e6 + 10);
    static Vector<Integer>[] node = new Vector[N];
    static Queue<Integer> queue = new LinkedList<>();
    static int[] dist = new int[N];
    static int[] ans = new int[N];
    static boolean[] str = new boolean[N];

    public static void main(String[] args) {
        new Thread(null, new Main(), "", 1 << 29).start();
    }

    public static int nextInt() throws IOException {
        cin.nextToken();
        return (int) cin.nval;
    }

    public static void bfs() {
        while(!queue.isEmpty()) {
            int val = queue.poll();
            for (Integer i : node[val]) {
                if(dist[val] == dist[i] + 1 && str[i]) {
                    ans[val] += ans[i];
                    ans[val] %= 100003;
                }
            }
            for (Integer num : node[val]) {
                if(!str[num]) {
                    str[num] = true;
                    queue.add(num);
                    dist[num] = dist[val] + 1;
                }
            }
        }
    }

    @Override
    public void run() {
        try {
            for (int i = 0; i < N; i++) node[i] = new Vector<>();
            n = nextInt();m = nextInt();
            for (int i = 1; i <= m; i++) {
                int x = nextInt();int y = nextInt();
                if(x == y) continue;
                node[x].add(y);node[y].add(x);
            }

            queue.add(1);str[1] = true;dist[1] = 0;ans[1] = 1;
            bfs();

//            for (int i = 1; i <= n; i++) System.out.print(dist[i] + " ");
//            System.out.println();
            for (int i = 1; i <= n; i++) cout.println(ans[i]);
            cout.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;

const int N = 1e6 + 10;
vector<int> node[N];
queue<int>q;
int dist[N];
int ans[N];
bool str[N];
int n, m;

void bfs() {
	while(q.size()) {
        int val = q.front();
        q.pop();
        for (int i : node[val]) {
            if(dist[val] == dist[i] + 1 && str[i]) {
                ans[val] += ans[i];
                ans[val] %= 100003;
            }
        }
        for (int num : node[val]) {
            if(!str[num]) {
                str[num] = true;
                q.push(num);
                dist[num] = dist[val] + 1;
            }
        }
    }	
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	
	cin>>n>>m;
    for (int i = 1; i <= m; i++) {
    	int x, y;
        cin>>x>>y;
        if(x == y) continue;
        node[x].push_back(y);node[y].push_back(x);
    }

    q.push(1);str[1] = true;dist[1] = 0;ans[1] = 1;
    bfs();

    for (int i = 1; i <= n; i++) cout<<ans[i]<<endl;

	
	return 0;

}

Dungeon Master - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

  • 感觉没啥问题,就是加了两个方向然后多了一个维度而已,但是总是WA,求调
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.*;

public class Main implements Runnable {

    static Scanner scanner = new Scanner(System.in);
//    static BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
    static int[] dx = {0, 1, 0, -1, 0, 0, 0};
    static int[] dy = {0, 0, 1, 0, -1, 0, 0};
    static int[] dz = {0, 0, 0, 0, 0, 1, -1};
    static int l, r, c, ans = 0;
    static final int N = 30;
    static char[][][] g = new char[N][N][N];
    static boolean[][][] str = new boolean[N][N][N];
    static int[][][] dist = new int[N][N][N];
    static Queue<node>queue = new LinkedList<>();


    public static void main(String[] args) {
        new Thread(null, new Main(), "", 1 << 29).start();
    }


    public static void bfs() {
        while(!queue.isEmpty()) {
            node t = queue.poll();
            for(int i = 1; i <= 6; i++) {
                int nowx = dx[i] + t.x;int nowy = dy[i] + t.y;int nowz = dz[i] + t.z;
                if (nowx < 1 || nowx > r || nowy < 1 || nowy > c || nowz < 1 || nowz > l || str[nowz][nowx][nowy] || g[nowz][nowx][nowy] == '#') continue;
                dist[nowz][nowx][nowy] = dist[t.z][t.x][t.y] + 1;
                if (g[nowz][nowx][nowy] == 'E'){
                    ans = dist[nowz][nowx][nowy];
                    cout.println("Escaped in " + ans + " minute(s).");
                    return;
                }

                str[nowz][nowx][nowy] = true;
                queue.add(new node(nowx, nowy, nowz));
            }
        }
        cout.println("Trapped!");
    }

//    public static void dfs(int z, int x, int y, int cnt) {
//        if(g[z][x][y] == 'E'){
//            ans = Math.min(cnt, ans);
//            return;
//        }
//        if(ans <= cnt) return;
//
//        for(int i = 1; i <= 6; i++) {
//            int nowx = dx[i] + x;int nowy = dy[i] + y;int nowz = dz[i] + z;
//            if (nowx < 1 || nowx > r || nowy < 1 || nowy > c || nowz < 1 || nowz > l || str[nowz][nowx][nowy] || g[nowz][nowx][nowy] == '#') continue;
//            str[nowz][nowx][nowy] = true;
//            dfs(nowz, nowx, nowy, cnt + 1);
//            str[nowz][nowx][nowy] = false;
//        }
//    }

    @Override
    public void run() {

        l = scanner.nextInt();r = scanner.nextInt();c = scanner.nextInt();
        while(l != 0 && r != 0 && c != 0) {
            int startX = 0, startY = 0, startZ = 0;
            for(int i = 1; i <= l; i++) {//z
                for(int j = 1; j <= r; j++) {//x
                    String s = scanner.next();
                    for(int k = 0; k < s.length(); k++){//y
                        if (s.charAt(k) == 'S') {
                            startZ = i;startX = j;startY = k + 1;
                        }
                        g[i][j][k + 1] = s.charAt(k);
                    }
                }
            }
            queue.add(new node(startX, startY, startZ));
            str[startZ][startX][startY] = true;
            bfs();

            ans = 0;
            l = scanner.nextInt();r = scanner.nextInt();c = scanner.nextInt();
            for (int i = 1; i <= l; i++) {
                for (int j = 1; j <= r; j++) {
                    Arrays.fill(str[i][j], false);
                    Arrays.fill(g[i][j], '0');
                }
            }
            queue.clear();
        }
        cout.flush();
    }

    static class node {
        public int x;
        public int y;
        public int z;

        public node(int x, int y, int z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }
    }
}
posted @ 2025-02-02 01:22  Mikkeykarl  阅读(20)  评论(0)    收藏  举报