light oj 1281 - New Traffic System (最短路+dp思想)

来源:互联网 发布:远程教育软件k6 编辑:程序博客网 时间:2024/06/06 15:41

1281 - New Traffic System
PDF (English)StatisticsForum
Time Limit: 2 second(s)Memory Limit: 32 MB

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 tare 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 tfrom 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

Output for 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

Case 1: 19

Case 2: Impossible

Note

Dataset is huge, use faster I/O methods.


题意:n个点,m条旧有向边,k条新有向边,现在求0到n-1的最短路,且最短路径上用到的新边数量不超过d。

思路:dis数组开二维,dis[i][j]表示到节点j用i条新边的最短路。

代码:

#include <iostream>#include <functional>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b)  for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b)  for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v)   memset ((t) , v, sizeof(t))#define sf(n)       scanf("%d", &n)#define sff(a,b)    scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf          printf#define DBG         pf("Hi\n")typedef long long ll;using namespace std;#define INF 0x3f3f3f3f#define mod 1000000009const int maxn = 1005;const int MAXN = 10010;const int MAXM = 200010;const int N = 1005;struct Edge{    int u,v,w,next,flag;}edge[MAXM];struct Node{    Node(){}    Node(int x,int y):cnt(x),u(y){}    int u,cnt;};int n,m,k,d;int head[MAXN],num,dis[11][MAXN],inq[11][MAXN];void init(){    num=0;    memset(head,-1,sizeof(head));}void addedge(int u,int v,int w,int f){    edge[num].u=u;    edge[num].v=v;    edge[num].w=w;    edge[num].flag=f;    edge[num].next=head[u];    head[u]=num++;}void SPFA(){    Node st;    memset(inq,false,sizeof(inq));    memset(dis,INF,sizeof(dis));    dis[0][0]=0;    inq[0][0]=true;    queue<Node>Q;    Q.push(Node(0,0));    while (!Q.empty())    {        st=Q.front();        Q.pop();        inq[st.cnt][st.u]=false;        for (int i=head[st.u];~i;i=edge[i].next)        {            int v=edge[i].v;            int tot=st.cnt+edge[i].flag;            if (tot>d) continue;            if (dis[tot][v]>dis[st.cnt][st.u]+edge[i].w)            {                dis[tot][v]=dis[st.cnt][st.u]+edge[i].w;                if (!inq[tot][v])                {                    inq[tot][v]=true;                    Q.push(Node(tot,v));                }            }        }    }    int ans=INF;    for (int i=0;i<=d;i++)        ans=min(dis[i][n-1],ans);    if (ans>=INF) printf("Impossible\n");    else printf("%d\n",ans);}int main(){#ifndef ONLINE_JUDGE    freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);#endif    int i,j,u,v,w,cas=0,t;    scanf("%d",&t);    while (t--)    {        scanf("%d%d%d%d",&n,&m,&k,&d);        init();        for (i=0;i<m;i++)        {            scanf("%d%d%d",&u,&v,&w);            addedge(u,v,w,0);        }        for (i=0;i<k;i++)        {            scanf("%d%d%d",&u,&v,&w);            addedge(u,v,w,1);        }        printf("Case %d: ",++cas);        SPFA();    }    return 0;}




0 0
原创粉丝点击