360 笔试0926

import java.util.*;

public class Main {
    static boolean[] used ;
    static int res;
    static void dfs(List<Integer>[] g, Map<Integer, Map<Integer,Integer>> map, int n, int m, int s, int t, int num) {
        if(used[s] == true) return ;
        if(s == t) {
            res = Math.min(res, num);
            return ; // 提交的时候忘了写,结果超时!!!!
        }
        for(int i=0; i < g[s].size(); i++) {
            used[s] = true;
            int v = g[s].get(i);
            int w = map.get(s).get(v);
            dfs(g,map,n,m,v,t, Math.max(num, w));
            used[s] = false;
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), m = sc.nextInt();
        int s = sc.nextInt(), t = sc.nextInt();
        Map<Integer, Map<Integer,Integer>> map = new HashMap<>();
        List<Integer>[] g = new ArrayList[n+1];
        for(int i=1; i <= n; i++) g[i] = new ArrayList<>();
        for(int i=0; i < m; i++) {
            int u = sc.nextInt(), v = sc.nextInt(), w = sc.nextInt();
            g[u].add(v);g[v].add(u);
            if(!map.containsKey(u)) map.put(u, new HashMap<>());
            if(!map.containsKey(v)) map.put(v, new HashMap<>());
            map.get(u).put(v, w);
            map.get(v).put(u, w);
        }
        used = new boolean[n+1];
        res = Integer.MAX_VALUE;
        dfs(g, map, n, m, s, t, 0);
        System.out.println(res);
    }

}
/*
5 6 1 5
1 5 100
1 2 10
2 5 5
1 3 3
3 4 2
4 5 1
第一行四个数n,m,s,t。

接下来m行,每行三个数u,v,w,表示u和v之间有一条边权为w的无向边。

输入保证s点和t点连通。

1≤n≤100,000;0≤m≤200,000;1≤w≤1,000,000,000
*/
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int a = sc.nextInt(), b = sc.nextInt();
            int k = sc.nextInt(), v = sc.nextInt();
            int res = 0;
            if(k > 1) {
                int t = b / (k-1);
                if(k*v*t >= a) {
                    res = a / (k*v);
                    if(a % (k*v) != 0) res++;
                    a = 0;
                } else {
                    res = t;
                    a -= k*v*t;

                }
                int t2 = b % (k-1);
                //System.out.println(t2 +" t2");
                if(a != 0 && t2 != 0) {
                    a-= (t2+1)*v;
                    res++;
                }
            }
            //a -= (b + 1)*v;
            if(a > 0) {
                res += a / v;
                if(a % v != 0) res++;
            }
            System.out.println(res);
        }


    }
/*
10 3 2 1
10 3 2 2
*/
}

原文地址:https://www.cnblogs.com/lixyuan/p/13736870.html