Gym 100646 You’ll be Working on the Railroad dfs
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ãn - 暴力
Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS Memory Limit:65536KB 64bit IO Fo ...
- ACM: Gym 101047K Training with Phuket'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 ...
随机推荐
- 考研:操作系统:进程同步—信号量实现同步互斥(PV操作)
进程互斥的硬件实现方法
- Spring RedisTemplate操作-Set操作(5)
@Autowired @Resource(name="redisTemplate") private RedisTemplate<String, String> rt; ...
- 20155306 2016-2017-2 《Java程序设计》第5周学习总结
20155306 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 异常处理 8.1 语法与继承架构 Java中所有错误都会被打包为对象,运用try.c ...
- XMPP 基础
CHENYILONG Blog XMPP 基础 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong ...
- Saving Tang Monk II
题目链接:http://hihocoder.com/contest/acmicpc2018beijingonline/problem/1 AC代码: #include<bits/stdc++.h ...
- Spring Boot 多模块项目创建与配置 (一)
最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块.代码中的多模块是用maven管理的,每个模块都使用spring boot框架.之前有零零散散学过一些 ...
- 关于sru源码class Model的parameters
class Model(nn.Module): def __init__(self, words, args): super(Model, self).__init__() self.args = a ...
- 【干货】SIFT-Workstation 下载与安装 不跳过每一个细节部分
SIFT-Workstation.ova 下载地址https://digital-forensics.sans.org/community/download-sift-kit ov ...
- JDK1.8源码Collections
正文: 一.概述: 此类完全由在 collection 上进行操作或返回 collection 的静态方法组成.它包含在 collection 上操作的多态算法,即“包装器”,包装器返回由指定 col ...
- brotli压缩
brotli压缩 https://www.cnblogs.com/shanyou/p/9154816.html Brotli是一种全新的数据格式,可以提供比Zopfli高20-26%的压缩比.据谷歌研 ...