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的更多相关文章

  1. Gym 100646 Problem C: LCR 模拟题

    Problem C: LCR 题目连接: http://codeforces.com/gym/100646/attachments Description LCR is a simple game f ...

  2. Gym 100646 Problem E: Su-Su-Sudoku 水题

    Problem E: Su-Su-Sudoku/center> 题目连接: http://codeforces.com/gym/100646/attachments Description By ...

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

  4. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  5. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  6. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  7. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

  8. Gym 101102J---Divisible Numbers(反推技巧题)

    题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...

  9. Gym 100917J---Judgement(01背包+bitset)

    题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...

随机推荐

  1. 一致性哈希算法介绍,及java实现

    应用场景 在做服务器负载均衡时候可供选择的负载均衡的算法有很多,包括: 轮循算法(Round Robin).哈希算法(HASH).最少连接算法(Least Connection).响应速度算法(Res ...

  2. html5 canvas 多个填充渐变形状

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 【问题收集·中级】关于XMPP使用Base传送图片

    [问题收集·中级]关于XMPP使用Base传送图片 下面是我与博友的问答过程:并在最后链接附录了相应的文件: 博友问题:  16:35:38 他跟我说要 内容图片  base64编码 上传..博友问题 ...

  4. python中的__getattr__、__getattribute__、__setattr__、__delattr__、__dir__

    __getattr__:     属性查找失败后,解释器会调用 __getattr__ 方法. class TmpTest: def __init__(self): self.tmp = 'tmp12 ...

  5. MySQL5.6主从复制最佳实践

    MySQL5.6     主从复制的配置  环境 操作系统:CentOS-6.6-x86_64 MySQL 版本:mysql-5.6.26.tar.gz 主节点 IP:192.168.31.57    ...

  6. rstful登陆认证并检查session是否过期

    一:restful用户视图 #!/usr/bin/env python # -*- coding:UTF-8 -*- # Author:Leslie-x from users import model ...

  7. 总结WCF开发中遇到的几个问题

    最近的项目,需要用到WCF,在以前的工作中,经常是将WCF托管在IIS中,主要有几下几个原因:      第一:部署非常方便,和部署一个站点没什么区别:      第二:不受防火墙的影响,因为一般服务 ...

  8. 钉钉机器人-实现监控通知功能-python

    1. 首先得创建有 一个 钉钉群.(因为只能发群通知) 2. 添加机器人,得到一个url: 3. 开始写Python脚本: # -*- coding: utf-8 -*- ""&q ...

  9. idea中搜狗输入法不跟随光标,看不到输入的字

    好久没在windows上开发了,今天遇到一个比较坑的问题: 最新版idea,输入法都是最新的;但是idea里面输入字,看不到自己输入的是什么字,好坑... 在外面可以看到输入什么字说明与输入法无关, ...

  10. tensorflow-训练(train)/测试(test)

    一个TFRecords 文件为一个字符串序列.这种格式并非随机获取,它比较适合大规模的数据流,而不太适合需要快速分区或其他非序列获取方式. 操作组 操作 Training Optimizers,Gra ...