[USACO09FEB]改造路Revamping Trails 分层最短路 Dijkstra BZOJ 1579
题意翻译
约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体.
通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 速公路.在高速公路上的通行几乎是瞬间完成的,所以高速公路的通行时间为0.
请帮助约翰决定对哪些小径进行升级,使他每天从1号牧场到第N号牧场所花的时间最短
题目描述
Farmer John dutifully checks on the cows every day. He traverses some of the M (1 <= M <= 50,000) trails conveniently numbered 1..M from pasture 1 all the way out to pasture N (a journey which is always possible for trail maps given in the test data). The N (1 <= N <= 10,000) pastures conveniently numbered 1..N on Farmer John's farm are currently connected by bidirectional dirt trails. Each trail i connects pastures P1_i and P2_i (1 <= P1_i <= N; 1 <= P2_i <= N) and requires T_i (1 <= T_i <= 1,000,000) units of time to traverse.
He wants to revamp some of the trails on his farm to save time on his long journey. Specifically, he will choose K (1 <= K <= 20) trails to turn into highways, which will effectively reduce the trail's traversal time to 0. Help FJ decide which trails to revamp to minimize the resulting time of getting from pasture 1 to N.
TIME LIMIT: 2 seconds
输入输出格式
输入格式:
* Line 1: Three space-separated integers: N, M, and K
* Lines 2..M+1: Line i+1 describes trail i with three space-separated integers: P1_i, P2_i, and T_i
输出格式:
* Line 1: The length of the shortest path after revamping no more than K edges
输入输出样例
说明
K is 1; revamp trail 3->4 to take time 0 instead of 100. The new shortest path is 1->3->4, total traversal time now 1.
比较裸的题目,与之相似的是:[JLOI2011]飞行路线
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 1000005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll qpow(ll a, ll b, ll c) {
ll ans = 1;
a = a % c;
while (b) {
if (b % 2)ans = ans * a%c;
b /= 2; a = a * a%c;
}
return ans;
} int n, m, k;
int d[maxn][30];
int vis[maxn][30];
vector<pii>vc[maxn];
int s, t; struct node {
int v, lev, dis;
node(){}
node(int v,int lev,int dis):v(v),lev(lev),dis(dis){}
bool operator < (const node&rhs)const {
return dis > rhs.dis;
}
}; priority_queue<node>q; int main()
{
//ios::sync_with_stdio(0);
rdint(n); rdint(m); rdint(k);
for (int i = 1; i <= m; i++) {
int u, v, w;
rdint(u); rdint(v); rdint(w);
vc[u].push_back(make_pair(v, w));
vc[v].push_back(make_pair(u, w));
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= k; j++)d[i][j] = inf;
}
d[1][0] = 0;
q.push(node(1, 0, 0));
while (!q.empty()) {
node tmp = q.top(); q.pop();
int to = tmp.v, lev = tmp.lev;
if (vis[to][lev])continue;
vis[to][lev] = 1;
vector<pii>::iterator it;
for (it = vc[to].begin(); it != vc[to].end(); it++) {
int y = (*it).first, w = (*it).second;
if (d[y][lev] > d[to][lev] + w) {
d[y][lev] = d[to][lev] + w;
q.push(node(y, lev, d[y][lev]));
}
if (lev < k) {
if (d[y][lev + 1] > d[to][lev]) {
d[y][lev + 1] = d[to][lev];
q.push(node(y, lev + 1, d[y][lev + 1]));
}
}
}
}
int ans = d[n][0];
for (int i = 1; i <= k; i++) {
ans = min(ans, d[n][i]);
}
cout << ans << endl;
return 0;
}
[USACO09FEB]改造路Revamping Trails 分层最短路 Dijkstra BZOJ 1579的更多相关文章
- P2939 [USACO09FEB]改造路Revamping Trails
P2939 [USACO09FEB]改造路Revamping Trails 同bzoj2763.不过dbzoj太慢了,bzoj又交不了. 裸的分层图最短路. f[i][j]表示免费走了j条路到达i的最 ...
- 洛谷 P2939 [USACO09FEB]改造路Revamping Trails 题解
P2939 [USACO09FEB]改造路Revamping Trails 题目描述 Farmer John dutifully checks on the cows every day. He tr ...
- 洛谷P2939 [USACO09FEB]改造路Revamping Trails
题意翻译 约翰一共有\(N\))个牧场.由\(M\)条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场\(1\)出发到牧场\(N\)去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰 ...
- [USACO09FEB] 改造路Revamping Trails | [JLOI2011] 飞行路线
题目链接: 改造路 飞行路线 其实这两道题基本上是一样的,就是分层图的套路题. 为什么是分层图呢?首先,我们的选择次数比较少,可以把这几层的图建出来而不会爆空间.然后因为选择一个边权为0的路线之后我们 ...
- P2939 [USACO09FEB]改造路Revamping Trails(分层图最短路)
传送门 完了我好像连分层图最短路都不会了……果然还是太菜了…… 具体来说就是记录一个步数表示免费了几条边,在dijkstra的时候以步数为第一关键字,距离为第二关键字.枚举边的时候分别枚举免不免费下一 ...
- 洛谷P2939 [USACO09FEB]改造路Revamping Trails(最短路)
题目描述 Farmer John dutifully checks on the cows every day. He traverses some of the M (1 <= M <= ...
- 分层图【p2939】[USACO09FEB]改造路Revamping Trails
Description 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小 ...
- [USACO09FEB]改造路Revamping Trails
题目描述 Farmer John dutifully checks on the cows every day. He traverses some of the M (1 <= M <= ...
- 洛谷 P2939 [USACO09FEB]改造路Revamping Trails
题意翻译 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 ...
随机推荐
- appium_python 实现手势密码
直接上代码吧: from appium.webdriver.common.touch_action import TouchAction from driver import AppiumTest # ...
- 将Windows下磁盘出现黑色为分配区域变成绿色区域
在windows下不知什么原因, 有一块磁盘空间F盘就变成了黑色为分配区域. 黑色区域无法用来安装双系统, 网上查阅资料后, 找到了如何将他重新变回绿色区域的2个方法(方法二是自己无意操作成功的). ...
- 第七章 Java中的13个原子操作类
当程序更新一个变量时,如果多线程同时更新这个变量,可能得到期望之外的值,比如变量i = 1:A线程更新i + 1,B线程也更新i + 1,经过两个线程操作之后可能i不等于3,而是等于2,.因为A和B线 ...
- DAY9-python并发之多进程理论
一.背景知识 顾名思义,进程即正在执行的一个过程.进程是对正在运行程序的一个抽象. 进程的概念起源于操作系统,是操作系统最核心的概念,也是操作系统提供的最古老也是最重要的抽象概念之一.操作系统的其他所 ...
- DevTools in Spring Boot 1.3
Spring Boot 1.3 will ship with a brand new module called spring-boot-devtools. The aim of this modul ...
- CDOJ1324-卿学姐与公主 【线段树点更新】
http://acm.uestc.edu.cn/#/problem/show/1324 卿学姐与公主 Time Limit: 2000/1000MS (Java/Others) Memory ...
- 【总结整理】js获取css的属性(内部,外部,内嵌(写在tag中))
在JS中需要获取某个元素的宽高或者是绝对定位的位置信息,通常我们会这么写: var elemWidth = elem.style.width; console.log(elemWidth); //(空 ...
- Maven 导包后,在Maven Dependencies 里面却没有相应的包
1 问题描述 在1处显示成功,但是在2处却没有相应的包 2 问题原因 查看pom.xml的源码,看你的依赖是否和我的方框中标签是一样的,有的会多出一个 xxxupdate 的标签(我这里给出的是正确 ...
- 解析IFC数据并转成json格式
{ "com.bim.ifc.ifc2x3.ifc2x3tc1.IfcBuilding (#104)-": [{ "objKey": "GlobalI ...
- Win10 VS2013 PCL1.8.1和依赖项VTK8.0.1, QHuall(2.15.2), FLANN1.9.1,Boost1.59.0,Zbil1.2.11和libPNG1.6.34编译安装
编译和安装过程最好使用管理员权限去操作,避免不必要的错误. 一般而言为了区分Debug和Release库,添加输入变量 Name: CMAKE_DEBUG_POSTFIX Type: STRING V ...