結果
| 問題 |
No.119 旅行のツアーの問題
|
| コンテスト | |
| ユーザー |
夕叢霧香(ゆうむらきりか)
|
| 提出日時 | 2018-01-15 19:08:14 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,869 bytes |
| コンパイル時間 | 2,853 ms |
| コンパイル使用メモリ | 83,608 KB |
| 実行使用メモリ | 51,192 KB |
| 最終ジャッジ日時 | 2024-12-24 02:40:40 |
| 合計ジャッジ時間 | 5,830 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 18 WA * 1 |
ソースコード
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;
}
}
夕叢霧香(ゆうむらきりか)