1087. All Roads Lead to Rome (30)
来源:互联网 发布:卖男生帽子的淘宝店铺 编辑:程序博客网 时间:2024/05/01 22:32
1087. All Roads Lead to Rome (30)
方法和1018类似,用path记录符合条件的路径(dijkstra),然后dfs筛选最优路径,最后打印路径
注:这里关于城市名称博主将它化为了整数储存,估计用map也不会超时
#include <cstdio>#include <vector>std::vector<int> s;std::vector<int> ans;std::vector<std::vector<int>> path(200+1);int vertex[200+1];int hp=0,route=0,cost=0,avghp=0;int TransToInterger(char s[]){ return (s[0]-'A')*10000+(s[1]-'A')*100+(s[2]-'A');}void dfs(std::vector<std::vector<int>> &path,int v){ s.push_back(v); if(v==0) { ++route; int curhp=0,c=-1; for(auto it=s.begin();it!=s.end();++it) { curhp+=vertex[*it]; ++c; } if(curhp>hp) { hp=curhp; avghp=curhp/c; ans=s; } else if(curhp==hp&&avghp<curhp/c) { hp=curhp; avghp=curhp/c; ans=s; } return; } for(auto it=path[v].begin();it!=path[v].end();++it) { dfs(path,*it); s.pop_back(); }}int main(){ const int INF=999999999; int vernum,arcnum; int adjmatrix[200+1][200+1]; int dist[200+1],visited[200+1]; int city[262626]; char vtocity[200+1][4]; scanf("%d %d %s",&vernum,&arcnum,vtocity[0]); city[TransToInterger(vtocity[0])]=0; for(int i=1;i<vernum;++i) { int happiness; scanf("%s %d",vtocity[i],&happiness); city[TransToInterger(vtocity[i])]=i; vertex[i]=happiness; } for(int i=0;i<vernum;++i) { visited[i]=0; for(int j=0;j<vernum;++j) { if(i==j) adjmatrix[i][j]=0; else adjmatrix[i][j]=INF; } } for(int i=0;i<arcnum;++i) { char vs[6],us[6]; int cost; scanf("%s %s %d",vs,us,&cost); int v=city[TransToInterger(vs)],u=city[TransToInterger(us)]; adjmatrix[v][u]=adjmatrix[u][v]=cost; } for(int i=0;i<vernum;++i) { if(adjmatrix[0][i]<INF) path[i].push_back(0); dist[i]=adjmatrix[0][i]; } visited[0]=1; int v,w,i,j; for(i=1;i<vernum;++i) { int min=INF; for(j=0;j<vernum;++j) { if(dist[j]<min&&!visited[j]) { min=dist[j]; v=j; } } visited[v]=1; for(w=0;w<vernum;++w) { if(!visited[w]&&adjmatrix[v][w]<INF) { if(dist[w]>dist[v]+adjmatrix[v][w]) { dist[w]=dist[v]+adjmatrix[v][w]; path[w].clear(); path[w].push_back(v); } else if(dist[w]==dist[v]+adjmatrix[v][w]) path[w].push_back(v); } } } int u=city[('R'-'A')*10000+('O'-'A')*100+('M'-'A')]; dfs(path,u); auto it=ans.begin()+1; int r=u; while(it!=ans.end()) { cost+=adjmatrix[r][*it]; r=*it; ++it; } printf("%d %d %d %d\n",route,cost,hp,avghp); while(--it>ans.begin()) printf("%s->",vtocity[*it]); printf("ROM"); return 0;}
0 0
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087. All Roads Lead to Rome (30)
- 1087.All Roads Lead to Rome (30)
- java基本数据类型
- python逐个遍历字符串(含汉字)
- 使用maven下载jar包慢优化
- VC++动态链接库(DLL)编程深入浅出(zz)
- STM32的SYSTICK详解
- 1087. All Roads Lead to Rome (30)
- 代理模式例子
- SSM,Spring,SpringMVC,Mybatis之Service---29
- 负载均衡方案
- iic系列问题
- JavaWeb小白笔记1—cookie
- 线性表插入
- 关于《爱程序网》转载文章没有任何转载说明的声明
- RC 522模块在LINUX平台调试笔记