-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFun.java
139 lines (127 loc) · 3.37 KB
/
Fun.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
/*
br = new BufferedReader(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
*/
public class Fun {
private static BufferedReader br;
private static StringTokenizer st;
private static PrintWriter pw;
public static void main(String[] args) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//int qq = 1;
//int qq = Integer.MAX_VALUE;
int qq = readInt();
for(int casenum = 1; casenum <= qq; casenum++) {
int v = readInt();
int e = readInt();
LinkedList<Edge>[] edges = new LinkedList[v];
for(int i = 0; i < v; i++) {
edges[i] = new LinkedList<Edge>();
}
int[] from = new int[e];
int[] to = new int[e];
int[] fun = new int[e];
int[] cost = new int[e];
for(int i = 0; i < e; i++) {
from[i] = readInt()-1;
to[i] = readInt()-1;
fun[i] = readInt();
cost[i] = readInt();
edges[from[i]].add(new Edge(to[i], fun[i], cost[i]));
}
double min = 0;
double max = 100;
final double INF = 1e20;
for(int qqq = 0; qqq < 30; qqq++) {
double mid = (min+max)/2;
double[] dp = new double[v];
Arrays.fill(dp, -INF);
dp[0] = 0;
for(int a = 0; a < v; a++) {
for(int i = 0; i < e; i++) {
if(dp[from[i]] == -INF) continue;
dp[to[i]] = Math.max(dp[to[i]], dp[from[i]] + fun[i] - cost[i] * mid);
}
}
LinkedList<Integer> q = new LinkedList<Integer>();
boolean[] seen = new boolean[v];
for(int i = 0; i < e; i++) {
if(dp[from[i]] == -INF) continue;
if(dp[from[i]] + fun[i] - cost[i] * mid > dp[to[i]] && !seen[to[i]]) {
q.add(to[i]);
dp[to[i]] = INF;
seen[to[i]] = true;
}
}
while(!q.isEmpty()) {
int curr = q.removeFirst();
for(Edge out: edges[curr]) {
if(!seen[out.d] && dp[curr] + out.f - out.c * mid > dp[out.d]) {
dp[out.d] = dp[curr] + out.f - out.c * mid;
seen[out.d] = true;
q.add(out.d);
}
}
}
if(dp[v-1] > 0) {
min = mid;
}
else {
max = mid;
}
}
pw.printf("%.6f\n", min);
}
exitImmediately();
}
static class Edge {
public int d, f, c;
public Edge(int d, int f, int c) {
super();
this.d = d;
this.f = f;
this.c = c;
}
}
private static void exitImmediately() {
pw.close();
System.exit(0);
}
private static long readLong() throws IOException {
return Long.parseLong(nextToken());
}
private static double readDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private static int readInt() throws IOException {
return Integer.parseInt(nextToken());
}
private static String nextLine() throws IOException {
if(!br.ready()) {
exitImmediately();
}
st = null;
return br.readLine();
}
private static String nextToken() throws IOException {
while(st == null || !st.hasMoreTokens()) {
if(!br.ready()) {
exitImmediately();
}
st = new StringTokenizer(br.readLine().trim());
}
return st.nextToken();
}
}