Cow and Exercise

Farmer John is obsessed with making Bessie exercise more!

Bessie is out grazing on the farm, which consists of $$$n$$$ fields connected by $$$m$$$ directed roads. Each road takes some time $$$w_i$$$ to cross. She is currently at field $$$1$$$ and will return to her home at field $$$n$$$ at the end of the day.

Farmer John has plans to increase the time it takes to cross certain roads. He can increase the time it takes to cross each road by a nonnegative amount, but the total increase cannot exceed $$$x_i$$$ for the $$$i$$$-th plan.

Determine the maximum he can make the shortest path from $$$1$$$ to $$$n$$$ for each of the $$$q$$$ independent plans.

Input

The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 50$$$, $$$1 \le m \le n \cdot (n-1)$$$) — the number of fields and number of roads, respectively.

Each of the following $$$m$$$ lines contains $$$3$$$ integers, $$$u_i$$$, $$$v_i$$$, and $$$w_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$1 \le w_i \le 10^6$$$), meaning there is an road from field $$$u_i$$$ to field $$$v_i$$$ that takes $$$w_i$$$ time to cross.

It is guaranteed that there exists a way to get to field $$$n$$$ from field $$$1$$$. It is guaranteed that the graph does not contain self-loops or parallel edges. It is possible to have a road from $$$u$$$ to $$$v$$$ and a road from $$$v$$$ to $$$u$$$.

The next line contains a single integer $$$q$$$ ($$$1 \le q \le 10^5$$$), the number of plans.

Each of the following $$$q$$$ lines contains a single integer $$$x_i$$$, the query ($$$0 \le x_i \le 10^5$$$).

Output

For each query, output the maximum Farmer John can make the shortest path if the total increase does not exceed $$$x_i$$$.

Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$.

Formally, let your answer be $$$a$$$, and the jury’s answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a – b|}{\max{(1, |b|)}} \le 10^{-6}$$$.

Example

input

3 3
1 2 2
2 3 2
1 3 3
5
0
1
2
3
4

output

3.0000000000
4.0000000000
4.5000000000
5.0000000000
5.5000000000

Solution:

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
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

vector<pair<int,ll>> ans;


namespace mincost {
    const int V=250,E=40000,inf=0x20202020,_inf=0x20;
    const ll inff=1ll<<60;
    ll dis[V],c[E],cost;
    int q[V*30],vis[V],fst[V],pre[V],nxt[E],y[E],f[E],S,T,flow,tot,tn;
    void init(int s,int t,int Tn) {
        tot=1; tn=Tn;
        rep(i,0,tn) fst[i]=0;
        S=s;T=t;
    }
    void add(int u,int v,int ff,ll cc) {
        tot++;y[tot]=v;nxt[tot]=fst[u];f[tot]=ff;c[tot]=cc;fst[u]=tot;
        tot++;y[tot]=u;nxt[tot]=fst[v];f[tot]=0;c[tot]=-cc;fst[v]=tot;
    }
    bool spfa() {
        rep(i,0,tn) dis[i]=inff,vis[i]=0,pre[i]=0;
        dis[S]=0;q[0]=S;vis[S]=1;
        int t=1;
        rep(i,0,t) {
            int u=q[i];
            for (int j=fst[u];j;j=nxt[j]) {
                int v=y[j];
                if (f[j]&&dis[v]>dis[u]+c[j]) {
                    dis[v]=dis[u]+c[j];
                    pre[v]=j;
                    if (!vis[v]) vis[v]=1,q[t++]=v;
                }
            }
            vis[u]=0;
        }
        return dis[T]!=inff;
    }
    void augment() {
        int p=T,_f=inf;
        while (pre[p]) _f=min(_f,f[pre[p]]),p=y[pre[p]^1];
        flow+=_f;cost+=_f*dis[T];
        p=T;
        while (pre[p]) f[pre[p]]-=_f,f[pre[p]^1]+=_f,p=y[pre[p]^1];
        ans.pb(mp(flow,cost));
    }
    void solve() {
        flow=0,cost=0;
        while (spfa()) augment();
    }
}

int n,m,u,v,w,q,x;
int main() {
    scanf("%d%d",&n,&m);
    mincost::init(0,n-1,n);
    rep(i,0,m) {
        scanf("%d%d%d",&u,&v,&w);
        mincost::add(u-1,v-1,1,w);
    }
    mincost::solve();
    scanf("%d",&q);
    rep(i,0,q) {
        scanf("%d",&x);
        db r=1e30;
        for (auto y:ans) r=min(r,1.*(y.se+x)/y.fi);
        printf("%.10f\n",r);
    }
}

Be the first to comment

Leave a Reply

Your email address will not be published.


*