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;
}