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

23 篇文章 0 订阅

1281 - New Traffic System
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 1000000009
const 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
杭州电子科技大学在线评测系统(杭电OJ)中的题目1000-1100是一系列编程题,我将分别进行回答。 1000题是一个简单的入门题,要求计算两个整数的和。我们可以使用一个简单的算法,读取输入的两个整数,然后将它们相加,最后输出结果即可。 1001题是一个稍微复杂一些的题目,要求实现字符串的逆序输出。我们可以使用一个循环来逐个读取输入的字符,然后将这些字符存储在一个数组中。最后,我们可以倒序遍历数组并将字符依次输出,实现字符串的逆序输出。 1002题是一个求最大公约数的问题。我们可以使用辗转相除法来解决,即先求出两个数的余数,然后将被除数更新为除数,将除数更新为余数,直至两个数的余数为0。最后的被除数就是最大公约数。 1003题是一个比较简单的排序问题。我们可以使用冒泡排序算法来解决,即每次比较相邻的两个元素,如果它们的顺序错误就交换它们的位置。重复这个过程直至整个数组有序。 1100题是一个动态规划问题,要求计算给定序列中的最长上升子序列的长度。我们可以使用一个数组dp来保存到达每个位置的最长上升子序列的长度。每当遍历到一个位置时,我们可以将其和之前的位置比较,如果比之前位置的值大,则将其更新为之前位置的值加1,最后返回dp数组的最大值即可。 以上是对杭电OJ1000-1100题目的简要回答,涉及了一些基本的编程知识和算法思想。希望对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值