You’ll be Working on the Railroad
题目连接:
http://codeforces.com/gym/100646/attachments
Description
Congratulations! 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.
Input
Input 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.
Output
For each test case, output a single line of the form
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.
Sample Input
7
0 2 10
0 3 6
2 4 5
3 4 3
3 5 4
4 1 7
5 1 8
0
Sample Output
0 3 4 1 3
Hint
题意
给你一个无向图,然后让你输出从1到n的最短路。
但是有一个人很有钱,他会帮你付最昂贵的两条路的价格;但是如果你只经过了一条边,他不会帮你付;如果你经过了两条边,他会帮你付最贵的。
题解:
数据范围很小,直接dfs搜就好了。
一条边和两条边的情况,直接暴力枚举就好了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 165;
int vis[maxn];
struct node{
int x,y;
node(int X,int Y):x(X),y(Y){};
};
vector<node> E[maxn];
vector<int> ans;
vector<int> tmp;
int Cost;
int n;
int cccc = 0;
void dfs(int x,int Ma1,int Ma2,int Ans){
if(Ans>Cost)return;
if(Ans==Cost&&tmp.size()>ans.size())return;
if(x==1){
if(tmp.size()<4)return;
if(Ans==Cost){
if(ans.size()==tmp.size()){
int flag = 0;
for(int i=0;i<ans.size();i++){
if(ans[i]>tmp[i]){
flag = 1;
break;
}
if(ans[i]<tmp[i])break;
}
if(flag==1){
ans=tmp;
}
}else ans=tmp;
}else{
ans=tmp;
Cost=Ans;
}
return;
}
for(int i=0;i<E[x].size();i++){
int v = E[x][i].x;
int y = E[x][i].y;
if(vis[v])continue;
tmp.push_back(v);
vis[v]=1;
if(y>Ma1)dfs(v,y,Ma1,Ans+Ma2);
else if(y>Ma2)dfs(v,Ma1,y,Ans+Ma2);
else dfs(v,Ma1,Ma2,Ans+y);
tmp.pop_back();
vis[v]=0;
}
}
int a[100],b[100],c[100];
void solve(){
cccc=0;
for(int i=0;i<maxn;i++)E[i].clear();
for(int i=1;i<=n;i++){
scanf("%d%d%d",&a[i],&b[i],&c[i]);
E[a[i]].push_back(node(b[i],c[i]));
E[b[i]].push_back(node(a[i],c[i]));
}
for(int i=0;i<150;i++)random_shuffle(E[i].begin(),E[i].end());
Cost = 1000000000;
ans.clear();
for(int i=0;i<2*n+5;i++)
ans.push_back(i);
memset(vis,0,sizeof(vis));
vis[0]=1;
tmp.clear();
tmp.push_back(0);
dfs(0,0,0,0);
for(int i=1;i<=n;i++){
if((a[i]==0&&b[i]==1)||(a[i]==1&&b[i]==0)){
tmp.clear();
tmp.push_back(0);
tmp.push_back(1);
if(Cost>c[i]){
Cost=c[i];
ans=tmp;
}else if(Cost==c[i]){
Cost=c[i];
if(ans.size()==tmp.size()){
int flag = 0;
for(int k=0;k<ans.size();k++){
if(ans[k]>tmp[k]){
flag = 1;
break;
}
if(ans[k]<tmp[k])break;
}
if(flag==1){
ans=tmp;
}
}else if(tmp.size()<ans.size())ans=tmp;
}
}
}
for(int i=1;i<=n;i++){
if(a[i]!=0&&b[i]!=0&&a[i]!=1&&b[i]!=1)continue;
for(int j=1;j<=n;j++){
if(a[j]!=0&&b[j]!=0&&a[j]!=1&&b[j]!=1)continue;
if(a[i]==0&&b[j]==1&&b[i]==a[j]){
tmp.clear();
tmp.push_back(0);
tmp.push_back(b[i]);
tmp.push_back(1);
if(Cost>min(c[i],c[j])){
Cost=min(c[i],c[j]);
ans=tmp;
}else if(Cost==min(c[i],c[j])){
Cost=min(c[i],c[j]);
if(ans.size()==tmp.size()){
int flag = 0;
for(int k=0;k<ans.size();k++){
if(ans[k]>tmp[k]){
flag = 1;
break;
}
if(ans[k]<tmp[k])break;
}
if(flag==1){
ans=tmp;
}
}else if(tmp.size()<ans.size())ans=tmp;
}
}
if(b[i]==0&&a[j]==1&&a[i]==b[j]){
tmp.clear();
tmp.push_back(0);
tmp.push_back(a[i]);
tmp.push_back(1);
if(Cost>min(c[i],c[j])){
Cost=min(c[i],c[j]);
ans=tmp;
}else if(Cost==min(c[i],c[j])){
Cost=min(c[i],c[j]);
if(ans.size()==tmp.size()){
int flag = 0;
for(int k=0;k<ans.size();k++){
if(ans[k]>tmp[k]){
flag = 1;
break;
}
if(ans[k]<tmp[k])break;
}
if(flag==1){
ans=tmp;
}
}else if(tmp.size()<ans.size())ans=tmp;
}
}
if(a[i]==0&&a[j]==1&&b[i]==b[j]){
tmp.clear();
tmp.push_back(0);
tmp.push_back(b[i]);
tmp.push_back(1);
if(Cost>min(c[i],c[j])){
Cost=min(c[i],c[j]);
ans=tmp;
}else if(Cost==min(c[i],c[j])){
Cost=min(c[i],c[j]);
if(ans.size()==tmp.size()){
int flag = 0;
for(int k=0;k<ans.size();k++){
if(ans[k]>tmp[k]){
flag = 1;
break;
}
if(ans[k]<tmp[k])break;
}
if(flag==1){
ans=tmp;
}
}else if(tmp.size()<ans.size())ans=tmp;
}
}
if(b[i]==0&&b[j]==1&&a[i]==a[j]){
tmp.clear();
tmp.push_back(0);
tmp.push_back(a[i]);
tmp.push_back(1);
if(Cost>min(c[i],c[j])){
Cost=min(c[i],c[j]);
ans=tmp;
}else if(Cost==min(c[i],c[j])){
Cost=min(c[i],c[j]);
if(ans.size()==tmp.size()){
int flag = 0;
for(int k=0;k<ans.size();k++){
if(ans[k]>tmp[k]){
flag = 1;
break;
}
if(ans[k]<tmp[k])break;
}
if(flag==1){
ans=tmp;
}
}else if(tmp.size()<ans.size())ans=tmp;
}
}
}
}
for(int i=1;i<=n;i++){
if(a[i]!=0&&b[i]!=0&&a[i]!=1&&b[i]!=1)continue;
for(int j=1;j<=n;j++){
if(a[j]!=0&&b[j]!=0&&a[j]!=1&&b[j]!=1)continue;
if(a[i]==1&&b[j]==0&&b[i]==a[j]){
tmp.clear();
tmp.push_back(0);
tmp.push_back(b[i]);
tmp.push_back(1);
if(Cost>min(c[i],c[j])){
Cost=min(c[i],c[j]);
ans=tmp;
}else if(Cost==min(c[i],c[j])){
Cost=min(c[i],c[j]);
if(ans.size()==tmp.size()){
int flag = 0;
for(int k=0;k<ans.size();k++){
if(ans[k]>tmp[k]){
flag = 1;
break;
}
if(ans[k]<tmp[k])break;
}
if(flag==1){
ans=tmp;
}
}else if(tmp.size()<ans.size())ans=tmp;
}
}
if(b[i]==1&&a[j]==0&&a[i]==b[j]){
tmp.clear();
tmp.push_back(0);
tmp.push_back(a[i]);
tmp.push_back(1);
if(Cost>min(c[i],c[j])){
Cost=min(c[i],c[j]);
ans=tmp;
}else if(Cost==min(c[i],c[j])){
Cost=min(c[i],c[j]);
if(ans.size()==tmp.size()){
int flag = 0;
for(int k=0;k<ans.size();k++){
if(ans[k]>tmp[k]){
flag = 1;
break;
}
if(ans[k]<tmp[k])break;
}
if(flag==1){
ans=tmp;
}
}else if(tmp.size()<ans.size())ans=tmp;
}
}
if(a[i]==1&&a[j]==0&&b[i]==b[j]){
tmp.clear();
tmp.push_back(0);
tmp.push_back(b[i]);
tmp.push_back(1);
if(Cost>min(c[i],c[j])){
Cost=min(c[i],c[j]);
ans=tmp;
}else if(Cost==min(c[i],c[j])){
Cost=min(c[i],c[j]);
if(ans.size()==tmp.size()){
int flag = 0;
for(int k=0;k<ans.size();k++){
if(ans[k]>tmp[k]){
flag = 1;
break;
}
if(ans[k]<tmp[k])break;
}
if(flag==1){
ans=tmp;
}
}else if(tmp.size()<ans.size())ans=tmp;
}
}
if(b[i]==1&&b[j]==0&&a[i]==a[j]){
tmp.clear();
tmp.push_back(0);
tmp.push_back(a[i]);
tmp.push_back(1);
if(Cost>min(c[i],c[j])){
Cost=min(c[i],c[j]);
ans=tmp;
}else if(Cost==min(c[i],c[j])){
Cost=min(c[i],c[j]);
if(ans.size()==tmp.size()){
int flag = 0;
for(int k=0;k<ans.size();k++){
if(ans[k]>tmp[k]){
flag = 1;
break;
}
if(ans[k]<tmp[k])break;
}
if(flag==1){
ans=tmp;
}
}else if(tmp.size()<ans.size())ans=tmp;
}
}
}
}
for(int i=0;i<ans.size();i++){
printf("%d ",ans[i]);
}
printf("%d\n",Cost);
}
int main(){
//freopen("1.in","r",stdin);
srand(time(NULL));
while(scanf("%d",&n)!=EOF){
if(n==0)break;
solve();
}
return 0;
}
Gym 100646 You’ll be Working on the Railroad dfs的更多相关文章
-
Gym 100646 Problem C: LCR 模拟题
Problem C: LCR 题目连接: http://codeforces.com/gym/100646/attachments Description LCR is a simple game f ...
-
Gym 100646 Problem E: Su-Su-Sudoku 水题
Problem E: Su-Su-Sudoku/center> 题目连接: http://codeforces.com/gym/100646/attachments Description By ...
-
Gym 100646 F Tanks a Lot RMQ
Problem F: Tanks a Lot Imagine you have a car with a very large gas tank - large enough to hold what ...
-
ACM: Gym 101047M Removing coins in Kem Kadr&#227;n - 暴力
Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS Memory Limit:65536KB 64bit IO Fo ...
-
ACM: Gym 101047K Training with Phuket&#39;s larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
-
ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
-
ACM: Gym 101047B Renzo and the palindromic decoration - 手速题
Gym 101047B Renzo and the palindromic decoration Time Limit:2000MS Memory Limit:65536KB 64 ...
-
Gym 101102J---Divisible Numbers(反推技巧题)
题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...
-
Gym 100917J---Judgement(01背包+bitset)
题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...
随机推荐
-
纪念逝去的岁月——C++实现一个栈(使用类模板)
这个版本是上个版本的加强版,上个版本的代码:http://www.cnblogs.com/fengbohello/p/4542912.html 目录 1.代码 2.运行结果 1.代码 1.1 调试信息 ...
-
jQuery的dom操作(二)转
addClass() 向匹配的元素添加指定的类名. after() 在匹配的元素之后插入内容. append() 向匹配的元素内部追加内容. appendTo() 向匹配的元素内部追加内容. attr ...
-
ListView优化相关
链接1 http://www.jb51.net/article/35273.htm 链接2 http://www.cnblogs.com/xilinch/archive/2012/11/08/2760 ...
-
EF,ADO.NET Entity Data Model简要的笔记
1. 新建一个项目,添加一个ADO.NET Entity Data Model的文件,此文件会生成所有的数据对象模型,如果是用vs2012生的话,在.Designer.cs里会出现“// Defaul ...
-
JavaScript函数部分
函数部分学习:+parseInt(): -parseInt()函数将其收到的任何输入值(通常是字符串)转换成整数类型输出,如果转换失败就返回NaN. -parseInt(“参数”,第二参数基数):没有 ...
-
201521123110 《Java程序设计》第1周学习总结
第一周学习总结 本周开始了对java的初次学习接触,Java是一门新的编程语言不同于C,由于有了c的基础,对于Java的理解和学习也相对从前学C更容易些. 也学习了Java的诞生发展以及运用包括JVN ...
-
jQueryUI使用指南
jQuery UI包含了许多维持状态的小部件(Widget),因此,它与典型的 jQuery 插件使用模式略有不同.所有的 jQuery UI 小部件(Widget)使用相同的模式,所以,只要您学会使 ...
-
Dynamic Programming | Set 3 (Longest Increasing Subsequence)
在 Dynamic Programming | Set 1 (Overlapping Subproblems Property) 和 Dynamic Programming | Set 2 (Opti ...
-
Codeforces Round #408 (Div. 2) 题解【ABCDE】
A - Buying A House 题意:给你n个房间,妹子住在第m个房间,你有k块钱,你想买一个离妹子最近的房间.其中相邻的房间之间距离为10,a[i]=0表示已经被别人买了. 题解:扫一遍更新答 ...
-
BZOJ 4321 queue2
4321: queue2 Description n 个沙茶,被编号 1~n.排完队之后,每个沙茶希望,自己的相邻的两人只要无一个人的编号和自己的编号相差为 1(+1 或-1)就行: 现在想知道,存在 ...