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. android tools相关

    1.showin 在include 的根节点设置,可一预览效果

  2. mybatis dao 层开发简易版 非整合 spring

    同样老习惯,先上项目结构截图 首先 补充上篇文中缺失的 mysql demo 用的 小脚本 drop database if exists mybatis; CREATE DATABASE `myba ...

  3. 【LibreOJ】#6392. 「THUPC2018」密码学第三次小作业 / Rsa 扩展欧几里得算法

    [题目]#6392. 「THUPC2018」密码学第三次小作业 / Rsa [题意]T次询问,给定正整数c1,c2,e1,e2,N,求正整数m满足: \(c_1=m^{e_1} \ \ mod \ \ ...

  4. NIO学习(1)-入门学习

    一.NIO概念 IO:标准IO,也既阻塞式IO NIO:非阻塞式IO 二.NIO与标准IO的IO工作方式 标准IO基于字节流和字符流进行操作 NIO是基于通道(Channel)和缓冲区(Buffer) ...

  5. 多源复制遇到CREATE USER FAILED错误

    MySQL Multi-Source Replication enables a replication slave to receive transactions from multiple sou ...

  6. mysql集成部署

    经常听说mysql数据库是集成在系统中,也一直不太明白集成的概念.今天才明白集成的概念就是将mysql所有的文件放到一个文件夹下放到系统中,也就是将mysql采用目录迁移部署的方式进行安装.在上一篇研 ...

  7. ESXi 6.5 总是会话超时

    ESXi 6.5 客户端Web界面会话超时 在VMware ESXi 6.5中,主机客户端Web界面会话每15分钟自动超时一次,然后您必须再次重新登录ESXi主机客户端Web界面. 要避免这种繁琐的情 ...

  8. 经典sql-获取当前文章的上一篇和下一篇

    我们在做资讯类的网站的时候,肯定会有这么一个需求,就是在资讯内容页的下方需要给出上一篇和下一篇资讯的链接.上次我一同事兼好友兼室友就遇到了这么一个需求,一开始我们都把问题想复杂了,先取的是符合条件的资 ...

  9. 『实践』Yalmip+Ipopt+Cplex使用手册

    Yalmip+Ipopt+Cplex使用手册 1.软件版本 Cplex 12.6.2,Matlab R2014a,Ipopt 3.12.9,Yalmip 2.Cplex添加方法 官方下载地址: htt ...

  10. Error updating database. Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String异常处理

    问题原因:Mybatis中对于时间参数进行比较时的一个BUG. 如果拿传入的时间类型参数与空字符串‘‘进行对比判断则会引发异常.,所以应该去掉该判断, 只保留非空判断就正常了 <if test= ...