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 <cstdio>
#include <vector>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const int INF=1<<28;
const int V_MAX=100;

struct edge{ int v,a,b,c; };

int main(){
	int n,m,s,t,F;
	while(~scanf("%d%d%d%d%d",&n,&m,&s,&t,&F) && n>0){
		vector<edge> G[V_MAX];
		rep(i,m){
			int u,v,a,b,c; scanf("%d%d%d%d%d",&u,&v,&a,&b,&c);
			G[u].push_back((edge){v,a,b,c});
		}

		edge z={-1}; // a<b の辺
		rep(u,n) rep(i,G[u].size()) {
			const edge &e=G[u][i];
			if(e.a<e.b) z=e;
		}

		static int d1[V_MAX][V_MAX]; // d1[u][v] := ( 流量 F の一本のフローを u -> v に流すための最小コスト )
		static int d2[V_MAX][V_MAX]; // 流量 z.c
		static int d3[V_MAX][V_MAX]; // 流量 F - z.c, ただし辺 z は通らない
		rep(u,n) rep(v,n) {
			d1[u][v]=(u==v?0:INF);
			d2[u][v]=(u==v?0:INF);
			d3[u][v]=(u==v?0:INF);
		}
		rep(u,n) rep(i,G[u].size()) {
			const edge &e=G[u][i];
			int v=e.v,a=e.a,b=e.b,c=e.c;
			if(F<=c) d1[u][v]=min(d1[u][v],a*F);
			else     d1[u][v]=min(d1[u][v],a*c+b*(F-c));
			if(z.v!=-1 && z.c<=F){
				if(z.c<=c) d2[u][v]=min(d2[u][v],a*z.c);
				else       d2[u][v]=min(d2[u][v],a*c+b*(z.c-c));
				if(a>=b){
					if((F-z.c)<=c) d3[u][v]=min(d3[u][v],a*(F-z.c));
					else           d3[u][v]=min(d3[u][v],a*c+b*((F-z.c)-c));
				}
			}
		}

		// Warshall-Floyd
		rep(w,n) rep(u,n) rep(v,n) d1[u][v]=min(d1[u][v],d1[u][w]+d1[w][v]);
		rep(w,n) rep(u,n) rep(v,n) d2[u][v]=min(d2[u][v],d2[u][w]+d2[w][v]);
		rep(w,n) rep(u,n) rep(v,n) d3[u][v]=min(d3[u][v],d3[u][w]+d3[w][v]);

		int ans=d1[s][t];
		rep(u,n) rep(v,n) ans=min(ans,d1[s][u]+d2[u][v]+d3[u][v]+d1[v][t]);

		if(ans<INF) printf("%d\n",ans); else puts("Impossible");
	}

	return 0;
}