天天看点

LightOJ - 1281 New Traffic System

Description

The country - Ajobdesh has a lot of problems in traffic system. As the Govt. is very clever (!), they made a plan to use only one way roads. Two cities s and t are the two most important cities in the country and mostly people travel from s to t. That's why the Govt. made a new plan to introduce some new one way roads in the traffic system such that the time to travel from s to t is reduced.

But since their budget is short, they can't construct more than d roads. So, they want to construct at most d new roads such that it becomes possible to reach t from s in shorter time. Unluckily you are one living in the country and you are assigned this task. That means you will be given the existing roads and the proposed new roads, you have to find the best path from s to t, which may allow at most d newly proposed roads.

Input

Input starts with an integer T (≤ 30), denoting the number of test cases.

Each case starts with a line containing four integers n (2 ≤ n ≤ 10000), m (0 ≤ m ≤ 20000), k (0 ≤ k ≤ 10000), d (0 ≤ d ≤ 10) where n denotes the number of cities, m denotes the number of existing roads and k denotes the number of proposed new roads. The cities are numbered from 0 to n-1 and city 0 is denoted as s and city (n-1) is denoted as t.

Each of the next m lines contains a description of a road, which contains three integers ui vi wi (0 ≤ ui, vi < n, ui ≠ vi, 1 ≤ wi ≤ 1000) meaning that there is a road from ui to vi and it takes wi minutes to travel in the road. There is at most one road from one city to another city.

Each of the next k lines contains a proposed new road with three integers ui vi wi (0 ≤ ui, vi < n, ui ≠ vi 1 ≤ wi ≤ 1000) meaning that the road will be from ui to vi and it will take wi minutes to travel in the road. There can be at most one proposed road from one city to another city.

Output

For each case, print the case number and the shortest path cost from s to t or "Impossible" if there is no path from s to t.

Sample Input

2

4 2 2 2

0 1 10

1 3 20

0 2 5

2 3 14

2 0 1 0

0 1 100

Sample Output

Case 1: 19

Case 2: Impossible

题解:新计划的地铁,有k个计划新路,利用现有的铁路、k条新路和限定只能用d条新路,找出从0到n-1的最短路径

用spfa(Shortest Path Faster Algorithm)算法来实现。通过每条路线和是否新路,计算‘0’结点到每个结点最短路径。distant[n-1][]一个结点有0~d个可能不同的最短路径,在d的范围找出最短的路径就是答案

#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;
const int INF = 0x3fffffff;
const int MAXN = 30005;
int t,n,m,k,d,u,v,w;
int inde = 0;
int head[10005]; //标记结点
int distant[10005][15]; //各个结点,二维是用多少新路

struct edge{
    int v,w,x,next;
}edges[30005];//路线

void add_edge(int u1,int v1,int w1,int x1){
    edges[inde].v = v1;
    edges[inde].w = w1;
    edges[inde].x = x1;
    edges[inde].next = head[u1];
    head[u1] = inde++;
}

struct point{
    int sum,u;
};

void spfa(){  //算出各结点的路径和使用得新路
    for(int i = 0; i < n; i++)
        for(int j = 0; j <= d; j++)
            distant[i][j] = INF;
    distant[0][0] = 0;
    point p;queue<point> Q;
    p.u = 0; p.sum = 0;
    Q.push(p);
    while(!Q.empty()){
        p = Q.front();Q.pop();
        for(int i = head[p.u];i!=-1;i=edges[i].next){
            int costRode = edges[i].w;
            int sumNewRode = p.sum+edges[i].x;
            int nextStartU = edges[i].v;
            point pp;
            pp.sum = sumNewRode;pp.u = nextStartU;
            if(costRode + distant[p.u][p.sum] < distant[nextStartU][sumNewRode]&&sumNewRode<=d){
                distant[nextStartU][sumNewRode] = costRode + distant[p.u][p.sum];
                Q.push(pp);
            }
        }
    }
}

int main(){
    cin>>t;
    for(int i = 1; i <= t;i++){
        cin>>n>>m>>k>>d;
        inde = 0;memset(head,-1,sizeof(head));
        while(m--)cin>>u>>v>>w,add_edge(u,v,w,0);
        while(k--)cin>>u>>v>>w,add_edge(u,v,w,1);
        spfa();int minn = INF;printf("Case %d: ",i);
        for(int j = 0; j <= d; j++){
            if(distant[n-1][j] < minn)minn = distant[n-1][j];
        }
        minn == INF?printf("Impossible\n"):printf("%d\n",minn);
    }
    return 0;
}
           

继续阅读