HDU 2783 You’ll be Working on the Railroad(最短路)
You’ll be Working on the Railroad
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 246 Accepted Submission(s): 63
Your county has just won a state grant to install a rail system between
the two largest towns in the county -- Acmar and Ibmar. This rail
system will be installed in sections, each section connecting two
different towns in the county, with the first section starting at Acmar
and the last ending at Ibmar. The provisions of the grant specify that
the state will pay for the two largest sections of the rail system, and
the county will pay for the rest (if the rail system consists of only
two sections, the state will pay for just the larger section; if the
rail system consists of only one section, the state will pay nothing).
The state is no fool and will only consider simple paths; that is, paths
where you visit a town no more than once. It is your job, as a recently
elected county manager, to determine how to build the rail system so
that the county pays as little as possible. You have at your disposal
estimates for the cost of connecting various pairs of cities in the
county, but you're short one very important requirement -- the brains to
solve this problem. Fortunately, the lackeys in the computing services
division will come up with something.
will contain multiple test cases. Each case will start with a line
containing a single positive integer n<=50 , indicating the number of
railway section estimates. (There may not be estimates for tracks
between all pairs of towns.) Following this will be n lines each
containing one estimate. Each estimate will consist of three integers s e
c , where s and e are the starting and ending towns and c is the
cost estimate between them. (Acmar will always be town 0 and Ibmar will
always be town 1. The remaining towns will be numbered using consecutive
numbers.) The costs will be symmetric, i.e., the cost to build a
railway section from town s to town e is the same as the cost to go
from town e to town s , and costs will always be positive and no
greater than 1000. It will always be possible to somehow travel from
Acmar to Ibmar by rail using these sections. A value of n = 0 will
signal the end of input.
c1 c2 ... cm cost
where
each ci is a city on the cheapest path and cost is the cost to the
county (note c1 will always be 0 and cm will always be 1 and ci and ci +
1 are connected on the path). In case of a tie, print the path with the
shortest number of sections; if there is still a tie, pick the path
that comes first lexicographically.
题意是给出 n 条边。
问从 0 -> 1 可以忽略2条路权,所需总权最小是多少,路径是什么 。
只有1条边时,不可忽略。
有2条时,可以忽略一条。
因为 n 是很少 , 直接暴力枚举可以忽略的边。
然后跑dij.来维护一个最优的路径。
这条题思路不难,不过就是写起来有点恶心
#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
#include <map>
#include <stack>
using namespace std ;
typedef pair<int,int> pii ;
#define X first
#define Y second
const int N = ;
const int inf = 1e9+ ;
typedef long long LL;
int n , xx[N] , pre[N] , ww[N] , tot ; vector<pii>g[N]; struct node {
int a , c , d , id ;
node(){};
node( int a , int c , int d , int id ):a(a),c(c),d(d),id(id){}
bool operator < ( const node &A ) const {
if( d != A.d ) return d > A.d ;
else if( c != A.c ) return c > A.c ;
else {
stack<int>s1 , s2; int id1 = id , id2 = A.id ;
while( id1 != - ) { s1.push( xx[id1] ); id1 = pre[id1] ; }
while( id2 != - ) { s2.push( xx[id2] ); id2 = pre[id2] ; }
while( !s1.empty() ) {
int a = s1.top() ; s1.pop() ;
int b = s2.top() ; s2.pop() ;
if( a > b ) return true ;
}
return false ;
}
}
};
int bestpath[N] , bestcnt , bestcost ;
int tmppath[N] , tmpcnt , tmpcost ; void Choose_best( int id , int cnt ) {
if( tmpcost > bestcost ) return ;
stack<int>s ;
while( id != - ) { s.push( xx[id] ); id = pre[id] ; }
tmpcnt = ;
while( !s.empty() ) { tmppath[tmpcnt++] = s.top() ; s.pop() ; } if( tmpcost < bestcost ) {
bestcnt = tmpcnt ;
bestcost = tmpcost ;
for( int i = ; i < tmpcnt ; ++i ) bestpath[i] = tmppath[i] ;
return ;
}
if( cnt > bestcnt ) return ;
else if( cnt < bestcnt ) {
bestcnt = tmpcnt ;
for( int i = ; i < tmpcnt ; ++i ) bestpath[i] = tmppath[i] ;
} else {
for( int i = ; i < tmpcnt ; ++i ) {
if( bestpath[i] > tmppath[i] ) {
for( int j = i ; j < bestcnt ; ++j ) {
bestpath[j] = tmppath[j] ;
}
} else if ( bestpath[i] < tmppath[i] ) {
break ;
}
}
}
} int dis[N] ; void dij() {
memset( dis , 0x3f , sizeof dis );
priority_queue<node>que;
tot = ; xx[tot] = , pre[tot] = - ; tot++ ;
que.push( node(,,,tot-) );
dis[] = ;
while( !que.empty() ) {
int u = que.top().a , cnt = que.top().c , cost = que.top().d , id = que.top().id ;
que.pop();
if( cost > dis[u] ) continue ;
if( u == ) {
if( dis[u] ) {
tmpcost = dis[u] ;
Choose_best( id , cnt );
}
return ;
}
for( int i = ; i < g[u].size() ; ++i ) {
int v = g[u][i].X , w = ww[g[u][i].Y] ;
if( dis[v] > dis[u] + w ) {
dis[v] = dis[u] + w ;
xx[tot] = v ; pre[tot] = id ; tot++;
que.push( node( v , cnt+ , dis[v] , tot - ) ) ;
}
}
}
} void Gao() {
dij();
for( int i = ; i < n ; ++i ) {
int cc = ww[i] ; ww[i] = ;
dij();
for( int j = i + ; j < n ; ++j ) {
int dd = ww[j] ; ww[j] = ;
dij(); ww[j] = dd ;
}
ww[i] = cc ;
}
for( int i = ; i < bestcnt ; ++i ) cout << bestpath[i] << ' ' ;
cout << bestcost << endl ;
} int Run() {
while( cin >> n && n ) {
bestcost = bestcnt = inf ;
for( int i = ; i < N ; ++i ) g[i].clear();
for( int i = ; i < n ; ++i ) {
int u , v , w ; cin >> u >> v >> w ;
ww[i] = w ;
g[u].push_back(pii(v,i));
g[v].push_back(pii(u,i));
}
Gao();
}
return ;
} int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
ios::sync_with_stdio();
return Run();
}
HDU 2783 You’ll be Working on the Railroad(最短路)的更多相关文章
- HDU 4725 The Shortest Path in Nya Graph(最短路拆点)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:n个点,某个点属于某一层.共有n层.第i层的点到第i+1层的点和到第i-1层的点的代价均是 ...
- HDU 6071 Lazy Running(很牛逼的最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=6071 题意: 1.2.3.4四个点依次形成一个环,现在有个人从2结点出发,每次可以往它相邻的两个结点跑,求最后回 ...
- HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]
HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...
- HDU 5669 线段树优化建图+分层图最短路
用线段树维护建图,即把用线段树把每个区间都标号了,Tree1中子节点有到达父节点的单向边,Tree2中父节点有到达子节点的单向边. 每次将源插入Tree1,汇插入Tree2,中间用临时节点相连.那么T ...
- hdu 5137 How Many Maos Does the Guanxi Worth 最短路 spfa
How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/5 ...
- HDU 2066 一个人的旅行(单源最短路SPFA)
Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还 ...
- HDU 3666 THE MATRIX PROBLEM (差分约束,最短路)
题意: 给一个n*m矩阵,每个格子上有一个数字a[i][j],给定L和U,问:是否有这样两个序列{a1...an}和{b1...bn},满足 L<=a[i][j]*ai/bj<=U .若存 ...
- hdu 1595 find the longest of the shortest【最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条】
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- HDU 4725 The Shortest Path in Nya Graph (最短路)
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
随机推荐
- LOJ6279 果树
我丢 之前sun在某校集训给我看过 当时也没想起来 今天补省集的锅的时候发现 wok这题我还听过?! 身败名裂.jpg (可是你记性不好这事情不已经人尽皆知了吗? 咳咳 回归正题 考虑对于两个同色的点 ...
- vue-cli项目中引入全局scss
加载一个全局设置文件 在每个组件里加载一个设置文件,而无需每次都将其显式导入,是一个常见的需求.比如为所有组件全局使用 scss 变量.为了达成此目的: npm install sass-resour ...
- Flask之 请求,应用 上下文源码解析
什么是上下文? 每一段程序都有很多外部变量.只有像Add这种简单的函数才是没有外部变量的.一旦你的一段程序有了外部变量,这段程序就不完整,不能独立运行.你为了使他们运行,就要给所有的外部变量一个一个写 ...
- Mike的农场
题目 Mike有一个农场,这个农场n个牲畜围栏,现在他想在每个牲畜围栏中养一只动物,每只动物可以是牛或羊,并且每个牲畜围栏中的饲养条件都不同,其中第i个牲畜围栏中的动物长大后,每只牛可以卖a[i]元, ...
- extjs定时器TaskRunner
Extjs封装了一个定时器类叫TaskRunner,该类在Ext.util下,先看看其公共方法: TaskRunner( Number interval ) interval : Number ( ...
- linux运维、架构之路-Kubernetes集群部署TLS双向认证
一.kubernetes的认证授权 Kubernetes集群的所有操作基本上都是通过kube-apiserver这个组件进行的,它提供HTTP RESTful形式的API供集群内外客户端调 ...
- BZOJ 1776: [Usaco2010 Hol]cowpol 奶牛政坛 LCA + 树的直径
Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) ...
- Dijkstra算法求最短路模板
Dijkstra算法适合求不包含负权路的最短路径,通过点增广.在稠密图中使用优化过的版本速度非常可观.本篇不介绍算法原理.只给出模板,这里给出三种模板,其中最实用的是加上了堆优化的版本 算法原理 or ...
- 数位dp浅谈(hdu3555)
数位dp简介: 数位dp常用于求区间内某些特殊(常关于数字各个数位上的值)数字(比如要求数字含62,49): 常用解法: 数位dp常用记忆化搜索或递推来实现: 由于记忆化搜索比较好写再加上博主比较蒟, ...
- 【Java】使用@Valid+BindingResult进行controller参数校验
@Valid @Valid注解用于校验,所属的包: javax.validation.Valid. 你可以定义实体,在实体的属性上添加校验规则,在API接收数据时添加@Valid注解,这时你的实体将会 ...