結果

問題 No.119 旅行のツアーの問題
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2018-01-15 19:08:14
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,869 bytes
コンパイル時間 2,601 ms
コンパイル使用メモリ 77,956 KB
実行使用メモリ 52,116 KB
最終ジャッジ日時 2023-08-25 17:17:11
合計ジャッジ時間 4,915 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
50,692 KB
testcase_01 AC 67 ms
50,536 KB
testcase_02 AC 54 ms
49,828 KB
testcase_03 AC 54 ms
49,960 KB
testcase_04 AC 55 ms
49,824 KB
testcase_05 AC 60 ms
50,672 KB
testcase_06 WA -
testcase_07 AC 55 ms
48,128 KB
testcase_08 AC 55 ms
49,908 KB
testcase_09 AC 56 ms
49,968 KB
testcase_10 AC 56 ms
50,088 KB
testcase_11 AC 55 ms
49,904 KB
testcase_12 AC 55 ms
50,032 KB
testcase_13 AC 56 ms
49,920 KB
testcase_14 AC 59 ms
50,544 KB
testcase_15 AC 60 ms
50,588 KB
testcase_16 AC 87 ms
52,000 KB
testcase_17 AC 56 ms
50,092 KB
testcase_18 AC 59 ms
50,536 KB
testcase_19 AC 57 ms
50,604 KB
testcase_20 AC 71 ms
50,772 KB
testcase_21 AC 78 ms
52,096 KB
testcase_22 AC 80 ms
52,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


class Main {
    static ArrayList<Long>[] g;
    static void add(int a, int b, int c){
        g[a].add((long)b<<32|c);
    }
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int n=sc.nextInt();
        int[]b=new int[n],c=new int[n];
        g=new ArrayList[3*n+2];
        Arrays.setAll(g,x->new ArrayList<Long>());
        for(int i=0;i<n;++i){
            b[i]=sc.nextInt();
            c[i]=sc.nextInt();
            add(0,2+i,1<<20);
            add(2+i,2+n+i,100-b[i]);
            add(2+n+i,2+i,1<<20);
            add(2+n+i,2+2*n+i,100);
            add(2+2*n+i,2+n+i,1<<20);
            add(2+2*n+i,1,100-c[i]);
        }
        int m=sc.nextInt();
        for(int i=0;i<m;++i){
            int d=sc.nextInt();
            int e=sc.nextInt();
            add(2+2*n+e,2+n+d,1<<20);
        }
        long ans=Dinic.maximumFlow(g,0,1);
        out.println(100*n-ans);
        out.close();
    }
    // http://codeforces.com/blog/entry/7018
    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out;
    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;
        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
        int[] nextIntArray(int n){
            int[]r=new int[n];
            for(int i=0;i<n;++i)r[i]=nextInt();
            return r;
        }
    }
}
/* 参考: http://www.prefield.com/algorithm/graph/dinic.html */
class Dinic{
    //#define RESIDUE(s,t) (capacity[s][t]-flow[s][t])
    static final long INF=1L<<55;
    static long augment(ArrayList<Long>[] g, long[][] capacity, long[][] flow,
                       int[] level, boolean[] finished, int u, int t, long cur) {
        if (u == t || cur == 0) return cur;
        if (finished[u]) return 0;
        finished[u] = true;
        for(long e:g[u]){
            int dst=(int)(e>>>32);
            int wei=(int)e;
            if (level[dst] > level[u]) {
                long f = augment(g, capacity, flow, level, finished,
                                 dst, t, Math.min(cur, capacity[u][dst]-flow[u][dst]));
                if (f > 0) {
                    flow[u][dst] += f; flow[dst][u] -= f;
                    finished[u] = false;
                    return f;
                }
            }
        }
        return 0;
    }
    // g[i][j] = (dest)<<32|(cap)
    static long maximumFlow(ArrayList<Long>[] g, int s, int t) {
        int n = g.length;
        long[][]flow=new long[n][n], cap=new long[n][n]; // adj. matrix
        for(int u=0;u<n;++u)
            for(long e:g[u]){
                int dst=(int)(e>>>32);
                int wei=(int)e;
                cap[u][dst] += wei;
            }
        
        long total = 0;
        for (boolean cont = true; cont; ) {
            cont = false;
            int[] level=new int[n];
            Arrays.fill(level, -1);
            level[s] = 0; // make layered network
            Queue<Integer> q=new ArrayDeque<Integer>();
            q.add(s);
            for (int d = n; q.size()>0 && level[q.peek()] < d; ) {
                int u=q.poll();
                if (u == t) d = level[u];
                for(long e:g[u]){
                    int dst=(int)(e>>>32);
                    int wei=(int)e;
                    if(cap[u][dst]>flow[u][dst] && level[dst] == -1) {
                        q.add(dst);
                        level[dst]=level[u]+1;
                    }
                }
            }
            boolean[] finished=new boolean[n];// make blocking flows
            for (long f = 1; f > 0; ) {
                f = augment(g, cap, flow, level, finished, s, t, INF);
                if (f == 0) break;
                total += f; cont = true;
            }
        }
        return total;
    }
}
0