Problem: G - Revenge of Minimum Cost Flow

Problem Statement

Flora is a freelance carrier pigeon. Since she is an excellent pigeon, there are too much task requests to her. It is impossible to do all tasks, so she decided to outsource some tasks to Industrial Carrier Pigeon Company.

There are N cities numbered from 0 to N-1. The task she wants to outsource is carrying f freight units from city s to city t. There are M pigeons in the company. The i-th pigeon carries freight from city s_i to t_i, and carrying cost of u units is u a_i if u is smaller than or equals to d_i, otherwise d_i a_i + (u-d_i)b_i. Note that i-th pigeon cannot carry from city t_i to s_i. Each pigeon can carry any amount of freights. If the pigeon carried freight multiple times, the cost is calculated from total amount of freight units he/she carried.

Flora wants to minimize the total costs. Please calculate minimum cost for her.

Input

The test case starts with a line containing five integers N (2 \leq N \leq 100), M (1 \leq M \leq 1{,}000), s (0 \leq s \leq N-1), t (0 \leq t \leq N-1) and f (1 \leq f \leq 200). You may assume s \neq t. Each of the next M lines contains five integers s_i (0 \leq s_i \leq N-1), t_i (0 \leq t_i \leq N-1), a_i (0 \leq a_i \leq 1{,}000), b_i (0 \leq b_i \leq 1{,}000) and d_i (1 \leq d_i \leq 200). Each denotes i-th pigeon's information. You may assume at most one pair of a_i and b_i satisfies a_i < b_i, all others satisfies a_i > b_i.

Output

Print the minimum cost to carry f freight units from city s to city t in a line. If it is impossible to carry, print "Impossible" (quotes for clarity).

Sample Input 1


2 2 0 1 5
0 1 3 0 3
0 1 2 1 6


Output for the Sample Input 1


9


Sample Input 2


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


Output for the Sample Input 2


18


Sample Input 3


2 1 0 1 1
1 0 1 0 1


Output for the Sample Input 3


Impossible


Sample Input 4


2 2 0 1 2
0 1 5 1 2
0 1 6 3 1


Output for the Sample Input 4


9


Sample Input 5


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


Output for the Sample Input 5


14






Example

Input

2 2 0 1 5
0 1 3 0 3
0 1 2 1 6


Output

9

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)
#define tpl(...) make_tuple(__VA_ARGS__)
const int INF = 1<<29;
const double EPS = 1e-8;
const double PI = acos(-1);
typedef long long ll;
typedef pair<int,int> pii;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
  os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
  os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}
template<int N,class Tuple> void out(ostream&,const Tuple&) {}
template<int N,class Tuple,class,class ...Ts> void out(ostream &os,const Tuple &t) {
	if(N)os<<","; os<<get<N>(t); out<N+1,Tuple,Ts...>(os,t);
}
template<class ...Ts> ostream& operator<<(ostream &os, const tuple<Ts...> &t) {
  os<<"("; out<0,tuple<Ts...>,Ts...>(os,t); os<<")"; return os;
}
template<class T>void output(T *a, int n) {REP(i,n){if(i)cout<<",";cout<<a[i];}cout<<endl;}
template<class T>void output(T *a, int n,int m) { REP(i,n) output(a[i],m);}

ll func(int a, int b, int d, int f) {
  return (f<=d ?
          f*a :
          d*a+(f-d)*b);
}

int x[1000];
int y[1000];
int a[1000];
int b[1000];
int d[1000];

ll distf[100][100];
ll distd[100][100];
ll distf_d[100][100];


int main() {
  int n,m,s,t,f;
  while(cin>>n>>m>>s>>t>>f) {
    REP(i,n)REP(j,n)
      distf_d[i][j]=distf[i][j]=distd[i][j]=(i==j?0:1LL<<50);
    int dd = INF;    
    REP(i,m) {
      cin >> x[i] >> y[i] >> a[i] >> b[i] >> d[i];
      if (a[i]<b[i]) dd = d[i];
    }
    if (dd != INF) {
      if (dd > f) dd = f;
    }
    // cout << tpl(f,dd,f-dd) << endl;
    REP(i,m) {
      chmin(distf[x[i]][y[i]],func(a[i],b[i],d[i],f));
      chmin(distd[x[i]][y[i]],func(a[i],b[i],d[i],dd));
      if (a[i]>b[i])
        chmin(distf_d[x[i]][y[i]],func(a[i],b[i],d[i],f-dd));      
    }
    REP(k,n)REP(i,n)REP(j,n) {
      chmin(distf[i][j], distf[i][k]+distf[k][j]);
      chmin(distd[i][j], distd[i][k]+distd[k][j]);
      chmin(distf_d[i][j], distf_d[i][k]+distf_d[k][j]);      
    }
    // cout << distd[0][2] << endl;
    // cout << distf_d[0][2] << endl;
    ll ans = 1LL<<50;
    if (dd == INF) {
      ans = distf[s][t];
    } else {    
      REP(i,n)REP(j,n) {
        chmin(ans, distf[s][i]+distd[i][j]+distf_d[i][j]+distf[j][t]);
      }
    }
    if (ans == 1LL<<50) {
      puts("Impossible");
    } else {
      cout << ans << endl;
    }
  }
}