题目链接:http://lightoj.com/volume_showproblem.php?problem=1281
题目给出n个城市m条已存在的道路,以及通过该道路需要的时间,然后就是k条可以修建的,但是由于预算的问题,最多只能修建d条道路,问这种情况下从1到n-1的最小时间花费。
不知道是被题目还是被自己逗了,,,真心没有读出来是单向边,wa得我都怀疑人生了。。。。
/*****************************************
Author :Crazy_AC(JamesQi)
Time :2015
File Name :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
/********************************************************/
/**********************Point*****************************/
/********************************************************/
/*
struct Point{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector A,Vector B){
return Vector(A.x + B.x,A.y + B.y);
}
Vector operator - (Vector A,Vector B){//向量减法
return Vector(A.x - B.x,A.y - B.y);
}
Vector operator * (Vector A,double p){//向量数乘
return Vector(A.x * p,A.y * p);
}
Vector operator / (Vector A,double p){//向量除实数
return Vector(A.x / p,A.y / p);
}
int dcmp(double x){//精度正负、0的判断
if (fabs(x) < eps) return 0;
return x < 0?-1:1;
}
bool operator < (const Point& A,const Point& B){//小于符号的重载
return A.x < B.x || (A.x == B.x && A.y < B.y);
}
bool operator == (const Point& A,const Point& B){//点重的判断
return dcmp(A.x - B.x) == 0&& dcmp(A.y - B.y) == 0;
}
double Dot(Vector A,Vector B){//向量的点乘
return A.x * B.x + A.y * B.y;
}
double Length(Vector A){//向量的模
return sqrt(Dot(A,A));
}
double Angle(Vector A,Vector B){//向量的夹角
return acos(Dot(A,B) / Length(A) / Length(B));
}
double Cross(Vector A,Vector B){//向量的叉积
return A.x * B.y - A.y * B.x;
}
double Area2(Point A,Point B,Point C){//三角形面积
return Cross(B - A,C - A);
}
Vector Rotate(Vector A,double rad){//向量的旋转
return Vector(A.x * cos(rad) - A.y * sin(rad),A.x * sin(rad) + A.y * cos(rad));
}
Vector Normal(Vector A){//法向量
int L = Length(A);
return Vector(-A.y / L,A.x / L);
}
double DistanceToLine(Point p,Point A,Point B){//p到直线AB的距离
Vector v1 = B - A,v2 = p - A;
return fabs(Cross(v1,v2)) / Length(v1);
}
double DistanceToSegment(Point p,Point A,Point B){//p到线段AB的距离
if (A == B) return Length(p - A);
Vector v1 = B - A, v2 = p - A,v3 = p - B;
if (dcmp(Dot(v1,v2 < 0))) return Length(v2);
else if (dcmp(Dot(v1,v3)) > 0) return Length(v3);
else return DistanceToLine(p,A,B);
}
Point GetLineProjection(Point p,Point A,Point B){//投影
Vector v = B - A;
return A + v * (Dot(v, p - A) / Dot(v, v));
}
bool SegmentProperIntersection(Point A1,Point A2,Point B1,Point B2){//线段相交
double c1 = Cross(A2 - A1,B1 - A1),c2 = Cross(A2 - A1,B2 - A1);
double c3 = Cross(B2 - B1,A1 - B1),c4 = Cross(B2 - B1,A2 - B1);
return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;
}
bool OnSegment(Point p,Point A,Point B){//点在线段上
return dcmp(Cross(A - p,B - p)) == 0 && dcmp(Dot(A - p,B - p)) < 0;
}
double PolygonArea(Point* p,int n){//多边形面积
double area = 0;
for (int i = 1;i < n - 1;++i){
area += Cross(p[i] - p[0],p[i + 1] - p[0]);
}
return area / 2;
}
double PolygonArea(vector<Point> p){//多边形面积
Point O(0,0);
double ret = 0;
p.push_back(p[0]);
for (int i = 0;i < p.size() - 1;++i){
ret += Cross(p[i] - O,p[i + 1] - O);
}
return ret / 2.0;
}
*/
/********************************************************/
/**********************Line******************************/
/********************************************************/
/*
struct Line{
Point p;//直线上任意一点
Vector v;//方向向量,它的左边就是半平面
double ang;//极角
Line(){}
// Line(Point p,Vector v):p(p),v(v){}
Line(Point A,Point B):p(A){
v = B - A;
ang = atan2(v.y,v.x);
}
bool operator < (const Line& rhs)const{
return ang < rhs.ang;
}
};
*/
const int maxn = 1e4 + 10;
struct Edge{
int to, cost;
bool newly;
Edge(int to,int cost,int newly):to(to),cost(cost),newly(newly){}
};
vector<Edge> G[maxn];
int n, m, k, d;
int dis[maxn][12];
int solve(){
int st = 0, ed = n - 1;
priority_queue<iii, vector<iii>, greater<iii> > que;
que.push(iii(ii(0, 0), 0));
memset(dis, INF,sizeof dis);
dis[0][0] = 0;
while(!que.empty()){
iii tmp = que.top();
que.pop();
int u = tmp.second;
int w = tmp.first.first;
int cnt = tmp.first.second;
if (u == ed) return dis[u][cnt];
if (w > dis[u][cnt]) continue;
if (cnt > d) continue;
int size = G[u].size();
for (int i = 0;i < size;++i){
int v = G[u][i].to;
int cost = G[u][i].cost;
bool is = G[u][i].newly;
if (is){
if (cnt + 1 <= d && dis[v][cnt + 1] > dis[u][cnt] + cost){
dis[v][cnt + 1] = dis[u][cnt] + cost;
que.push(iii(ii(dis[v][cnt + 1],cnt + 1), v));
}
}else{
if (dis[v][cnt] > dis[u][cnt] + cost){
dis[v][cnt] = dis[u][cnt] + cost;
que.push(iii(ii(dis[v][cnt], cnt), v));
}
}
}
}
int ans = INF;
// for (int i = 0;i <= d;++i)
// ans = min(ans, dis[n-1][i]);
return ans;
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int t, icase = 0;
scanf("%d",&t);
while(t--){
scanf("%d%d%d%d",&n,&m,&k,&d);
for (int i = 0;i < n;++i)
G[i].clear();
int u, v, w;
for (int i = 0;i < m;++i){
scanf("%d%d%d",&u,&v,&w);
G[u].push_back(Edge(v, w, false));
// G[v].push_back(Edge(u, w, false));
}
for (int i = 0;i < k;++i){
scanf("%d%d%d",&u,&v,&w);
G[u].push_back(Edge(v, w, true));
// G[v].push_back(Edge(u, w, true));
}
int ans = solve();
printf("Case %d: ", ++icase);
if (ans == INF) puts("Impossible");
else printf("%d\n", ans);
}
return 0;
}