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

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.

思路:最短路;

这个是二维最短路,因为可以添加一些边,而且边数有限制,d[i][j]表示到0点到当前点添加条边的最短路径,用个优先队列维护的dj算法。

  1 #include <cstdio>
2 #include <cstdlib>
3 #include <cstring>
4 #include <cmath>
5 #include <iostream>
6 #include <algorithm>
7 #include <map>
8 #include <queue>
9 #include <vector>
10 using namespace std;
11 typedef long long LL;
12 typedef struct pp
13 {
14 int from;
15 int to;
16 int cost;
17 int time;
18 bool operator<(const pp&cx)const
19 {
20 return cx.cost<cost;
21 }
22 } ss;
23 vector<ss>vec[20005];
24 vector<ss>avec[20005];
25 int dis[11][20000];
26 priority_queue<ss>que;
27 bool flag[11][20000];
28 ss mark[20000];
29 void dj(int p);
30 int main(void)
31 {
32 int i,j,k;
33 int __ca=0;
34 scanf("%d",&k);
35 int n,m,s,d;
36 while(k--)
37 {
38 __ca++;
39 scanf("%d %d %d %d",&n,&m,&s,&d);
40 for(i=0; i<20000; i++)
41 {
42 mark[i].time=0;
43 mark[i].cost=1e9;
44 }
45 while(!que.empty())
46 que.pop();
47 for(i=0; i<20005; i++)
48 {
49 vec[i].clear();
50 avec[i].clear();
51 }
52 while(m--)
53 {
54 int x,y,co;
55 scanf("%d %d %d",&x,&y,&co);
56 ss aa;
57 aa.from=x;
58 aa.to=y;
59 aa.cost=co;
60 vec[x].push_back(aa);
61 }
62 for(i=0; i<s; i++)
63 {
64 int x,y,co;
65 scanf("%d %d %d",&x,&y,&co);
66 ss aa;
67 aa.from=x;
68 aa.to=y;
69 aa.cost=co;
70 avec[x].push_back(aa);
71 }
72 memset(flag,0,sizeof(flag));
73 dj(d);
74 int maxx=1e9;
75 for(i=0; i<=d; i++)
76 {
77 if(maxx>dis[i][n-1])
78 maxx=dis[i][n-1];
79 }
80 if(maxx==1e9)
81 {
82 printf("Case %d: Impossible\n",__ca);
83 }
84 else
85 {
86 printf("Case %d: %d\n",__ca,maxx);
87 }
88 }
89 return 0;
90 }
91 void dj(int p)
92 {
93 int i,j;
94 for(i=0; i<11; i++)
95 {
96 for(j=0; j<20000; j++)
97 {
98 dis[i][j]=1e9;
99 }
100 dis[i][0]=0;
101 }
102 dis[0][0]=0;
103 ss ak;
104 ak.to=0;
105 ak.cost=0;
106 ak.time=0;
107 que.push(ak);
108 while(!que.empty())
109 {
110 ss a=que.top();
111 que.pop();
112 int to=a.to;
113 int time=a.time;
114 int co=a.cost;
115 if(dis[time][to]<co||flag[time][to])
116 continue;
117 else
118 {
119 flag[time][to]=true;
120 dis[time][to]=co;
121 for(i=0; i<vec[to].size(); i++)
122 {
123 ss ac=vec[to][i];
124 if(dis[time][ac.to]>co+ac.cost)
125 {
126 dis[time][ac.to]=co+ac.cost;
127 ss dd;
128 dd.to=ac.to;
129 dd.cost=co+ac.cost;
130 dd.time=time;
131 que.push(dd);
132 }
133 }
134 if(time<p)
135 {
136 for(i=0; i<avec[to].size(); i++)
137 {
138 ss ac=avec[to][i];
139 if(dis[time+1][ac.to]>co+ac.cost)
140 {
141 dis[time+1][ac.to]=co+ac.cost;
142 ss dd;
143 dd.to=ac.to;
144 dd.cost=co+ac.cost;
145 dd.time=time+1;
146 que.push(dd);
147 }
148 }
149 }
150 }
151 }
152 }

1281 - New Traffic System的更多相关文章

  1. Light oj 1281 - New Traffic System 多状态最短路

    题目大意:有向图,新计划的地铁,有k个计划新路,利用现有的铁路.k条新路和限定只能用d条新路,找出从0到n-1的最短路径 题目思路:用dist[u][use],储存使用use条新路,到达节点u的最短路 ...

  2. HDU 4744 Starloop System(最小费用最大流)(2013 ACM/ICPC Asia Regional Hangzhou Online)

    Description At the end of the 200013 th year of the Galaxy era, the war between Carbon-based lives a ...

  3. UVALive 2664 One-way traffic

    One-way traffic Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Or ...

  4. LightOJ 1291 Real Life Traffic

    Real Life Traffic Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on LightOJ. O ...

  5. Java实现One-way traffic(单向交通)

    One-way traffic In a certain town there are n intersections connected by two- and one-way streets. T ...

  6. Android软件测试Monkey测试工具

    前言: 最近开始研究Android自动化测试方法,对其中的一些工具.方法和框架做了一些简单的整理,其中包括android测试框架.CTS.Monkey.Monkeyrunner.benchmark.其 ...

  7. Naming Conventions for .NET / C# Projects

    http://www.akadia.com/services/naming_conventions.html Naming Conventions for .NET / C# Projects Mar ...

  8. Java: 基类、子类、构造函数、程序块的初始化顺序

    初始化顺序 基类static block 子类static block 基类non-static block 子类non-static block 基类constructor 子类constructo ...

  9. Big Data Analytics for Security(Big Data Analytics for Security Intelligence)

    http://www.infoq.com/articles/bigdata-analytics-for-security This article first appeared in the IEEE ...

随机推荐

  1. 详解getchar()函数与缓冲区

    1.首先,我们看一下这段代码: 它的简单意思就是从键盘读入一个字符,然后输出到屏幕.理所当然,我们输入1,输出就是1,输入2,输出就是2. 那么我们如果输出的是12呢? 它的输出是1. 这里我们先简单 ...

  2. 开始读 Go 源码了

    原文链接: 开始读 Go 源码了 学完 Go 的基础知识已经有一段时间了,那么接下来应该学什么呢?有几个方向可以考虑,比如说 Web 开发,网络编程等. 在下一阶段的学习之前,写了一个开源项目|Go ...

  3. flink---实时项目--day02-----1. 解析参数工具类 2. Flink工具类封装 3. 日志采集架构图 4. 测流输出 5. 将kafka中数据写入HDFS 6 KafkaProducer的使用 7 练习

    1. 解析参数工具类(ParameterTool) 该类提供了从不同数据源读取和解析程序参数的简单实用方法,其解析args时,只能支持单只参数. 用来解析main方法传入参数的工具类 public c ...

  4. 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(七)-准备移植FatFs

    [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...

  5. Android EditText软键盘显示隐藏以及“监听”

    一.写此文章的起因 本人在做类似于微信.易信等这样的聊天软件时,遇到了一个问题.聊天界面最下面一般类似于如图1这样(这里只是显示了最下面部分,可以参考微信等),有输入文字的EditText和表情按钮等 ...

  6. Servlet(3):Cookie和Session

    一. Cookie Cookie是客户端技术,程序把每个用户的数据以cookie的形式写给用户各自的浏览器.当用户使用浏览器再去访问服务器中的web资源时,就会带着各自的数据去.这样,web资源处理的 ...

  7. 使用jstl和el表达式来展示request域中存放的user对象的信息

    <%@ page import="java.util.ArrayList" %><%@ page import="java.util.List" ...

  8. 【HarmonyOS】【DevEco Studio】NOTE05:PageAbility生命周期的呈现

    NOTE05:PageAbility生命周期的呈现 基本界面设置 创建Slice与对应xml BarAbilitySlice package com.example.myapplication.sli ...

  9. SpringBoot 2.x 自定义拦截器并解决静态资源访问被拦截问题

      自定义拦截器 /** * UserSecurityInterceptor * Created with IntelliJ IDEA. * Author: yangyongkang * Date: ...

  10. 『学了就忘』Linux系统管理 — 85、工作管理相关命令

    目录 1.工作管理简介 2.如何把命令放入后台 3.后台管理命令 (1)查看后台的工作 (2)将后台暂停的工作恢复到前台执行 (3)把后台暂停的工作恢复到后台执行 4.后台命令脱离登录终端运行 1.工 ...