1087. All Roads Lead to Rome (30)
#include <iostream>
#include <vector>
#include <string.h>using namespace std;struct node
{int next, cost;
};vector<node> v[27000];
vector<int> path, res;
char name[27000][5];
int happ[27000], vis[27000], mincost = 100000000, maxsumhapp = -1, endindex, same;
double maxavghapp;int getbigindex(char s[])
{return (s[2] - 'A') + (s[1] - 'A') * 26 + (s[0] - 'A') * 26 * 26;
}void dfs(int curindex, int curcost, int cursumhapp, int curcount)
{int flag = 0;double curavghapp;if(curindex == endindex){curavghapp = cursumhapp * 1.0 / curcount;if(curcost < mincost){flag = 1;same = 1;}else if(curcost == mincost){same++;if(cursumhapp > maxsumhapp){flag = 1;}else if(cursumhapp == maxsumhapp){if(curavghapp > maxavghapp){flag = 1;}}}if(flag == 1){mincost = curcost;maxsumhapp = cursumhapp;maxavghapp = curavghapp;res = path;}return;}int size = v[curindex].size(), i, next, nextcost, nextsumhapp, nextcount;for(i = 0; i < size; i++){next = v[curindex][i].next;nextcost = v[curindex][i].cost + curcost;nextsumhapp = happ[next] + cursumhapp;nextcount = curcount + 1;if(vis[next] == 0 && nextcost <= mincost){path.push_back(next);vis[next] = 1;dfs(next, nextcost, nextsumhapp, nextcount);path.pop_back();vis[next] = 0;}}
}int main()
{int n, k;char begin[5];scanf("%d%d%s", &n, &k, &begin);int beginindex = getbigindex(begin);strcpy(name[beginindex], begin);int i, curhapp, cityindex;char cityname[5];for(i = 1; i < n; i++){getchar();scanf("%s%d", cityname, &curhapp);cityindex = getbigindex(cityname);strcpy(name[cityindex], cityname);happ[cityindex] = curhapp;}char a[5], b[5];node nod;int index0, index1;for(i = 1; i <= k; i++){getchar();scanf("%s%s%d", a, b, &nod.cost);index0 = getbigindex(a);index1 = getbigindex(b);nod.next = index1;v[index0].push_back(nod);nod.next = index0;v[index1].push_back(nod);}endindex = getbigindex("ROM");path.push_back(beginindex);vis[beginindex] = 1;dfs(beginindex, 0, 0, 0);printf("%d %d %d %d\n", same, mincost, maxsumhapp, (int)maxavghapp);int size = res.size();for(i = 0; i < size; i++){if(i > 0){printf("->");}printf("%s", name[res[i]]);}printf("\n");system("pause");return 0;
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/974075.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!