别名“黑人战神”

发表题解,造福后人!!!

博客园 首页 新随笔 联系 订阅 管理

New Traffic System


Time Limit: 2000 ms     Case Time Limit: 2000 ms     Memory Limit: 32768 KB
Submit: 10     Accepted: 1
This problem will be judged on LightOJ. Original ID: 1281.

 

[Prev][Next]

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

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

Sample Output

Source

Jane Alam Jan

题目大意:n个点,初始有m条路,给出新修建k条路。求从1到n的最短路,要求经过新路的的个数不大于d,当然可以走初始的路。

输入:n,m,k,d.

输出:若有解,输出最短路。无解输出“Impossible”。

 

解法:用一个二维数组d[i][j]表示i这个点经过j条新路达到的最短路,vis[i][j]类似。其余的部分完全和一维最短路一样。

 

#include <algorithm>
#include<cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include<iterator>
using namespace std;
#define N 90000
#define M 20000
#define INF 1<<29
#define exp (1e-8)
#define mm(a,b) memset((a),b,sizeof(a))
#define PI 3.1415926535898
#define E 2.718281828459
#define LL long long
using namespace std;

struct Edge
{
    int to,next,val;
    int check;
}edge[N];

struct point
{
    int id,cnt;
};

int head[N],k,n,m,k1,d1,cas=0;
int d[N][20];
bool vis[N][20];

void init()
{
    mm(head,0);
    k=0;
}

void addedge(int from,int to,int val,int check)
{
    edge[++k].to=to;
    edge[k].val=val;
    edge[k].next=head[from];
    edge[k].check=check;
    head[from]=k;
}

void spfa()
{
    queue<point >q;
    mm(vis,0);
    for(int i=0;i<=n;i++)
    {
        for(int j=0;j<=15;j++)
        {
            d[i][j]=INF;
        }
    }
    d[0][0]=0;
    vis[0][0]=1;
    point x;
    x.id=0;
    x.cnt=0;
    q.push(x);
    while(!q.empty())
    {
        point now=q.front();
        q.pop();
        int a1,a2;
        a1=now.id;
        a2=now.cnt;
        vis[a1][a2]=0;
        for(int i=head[a1];i;i=edge[i].next)
        {
            x.id=edge[i].to;
            x.cnt=a2+edge[i].check;
            if(d[x.id][x.cnt]>d[a1][a2]+edge[i].val)
            {
                d[x.id][x.cnt]=d[a1][a2]+edge[i].val;
                if(!vis[x.id][x.cnt])
                {
                    vis[x.id][x.cnt]=1;
                    q.push(x);
                    //else q.push_back(to);
                }
            }
        }
    }
    printf("Case %d: ",++cas);
    int mini=INF;
    for(int i=0;i<=d1;i++)
    {
        mini=min(mini,d[n-1][i]);
    }
    if(mini==INF)
    请勿直接复制代码,对你没与任何好处!
        printf("Impossible\n");
    else printf("%d\n",mini);
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d",&n,&m,&k1,&d1);
        init();
        for(int i=1;i<=m;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            //cout<<c<<endl;
            addedge(a,b,c,0);
            //addedge(b,a,c,0);
        }
        for(int i=1;i<=k1;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            addedge(a,b,c,1);
            //addedge(b,a,c,1);
        }
        //cout<<k1<<" "<<d1<<endl;
        spfa();
    }
    return 0;
}

 

posted on 2014-03-12 18:19  别名“黑人战神”  阅读(199)  评论(0)    收藏  举报