牛客网浮点错误怎么解决?

qq_42153923 2019-04-28 08:12:41
牛客网上的pat题目:All Roads Lead to Rome,我在自己的vc6.0上没有问题,牛客网显示浮点错误? 把浮点错误去掉之后还超时。。。大佬救救孩子

#include <iostream>
#include <vector>
#include <string>
#include <limits.h>

using namespace std;

int N,K,rome; //the number of cities and routes
string start;
vector<string> city;
vector<int> happiness;
vector<vector<int> > cost;
vector<bool> visit;

int bests, costs, happy, ave_happy;
int correctHappy, correctAveHappy;
int correctCosts=INT_MAX;
vector<int> route,correctRoute;

void dfs(int pre, int present, int end);

int main()
{
int i,hap;
string cit;
ios::sync_with_stdio(false);
cin>>N>>K>>start;
city.resize(N,start);
happiness.resize(N,0);
cost.resize(N,vector<int>(N,0));
visit.resize(N,false);
for(i=1;i<N;i++)
{
cin>>cit>>hap;
if(cit=="ROM")
rome=i;
city[i]=cit;
happiness[i]=hap;
}
string city1,city2;
int cos,j,m,n;
for(i=0;i<K;i++)
{
cin>>city1>>city2>>cos;
for(j=0;j<N;j++)
if(city1==city[j])
{
m=j;
break;
}
for(j=0;j<N;j++)
if(city2==city[j])
{
n=j;
break;
}
cost[m][n]=cos;
cost[n][m]=cos;
}
dfs(0,0,rome);
cout<<bests<<" "<<correctCosts<<" "<<correctHappy<<" "<<correctAveHappy<<endl;
cout<<city[correctRoute[0]];
for(i=1;i<correctRoute.size();i++)
cout<<"->"<<city[correctRoute[i]];
cout<<endl;

return 0;
}

void dfs(int pre, int present, int end)
{
if(costs>correctCosts)
return;
int i;
visit[present]=true;
costs+=cost[pre][present];
happy+=happiness[present];
route.push_back(present);

if(present==end)
{
ave_happy=happy/(route.size()-1);
if(costs<correctCosts)
{
bests=1;
correctCosts=costs;
correctRoute=route;
correctHappy=happy;
correctAveHappy=ave_happy;
return;
}
else if(costs==correctCosts)
{
bests++;
if(happy>correctHappy)
{
correctCosts=costs;
correctRoute=route;
correctHappy=happy;
correctAveHappy=ave_happy;
return;
}
else if(happy==correctHappy)
if(ave_happy>correctAveHappy)
{
correctCosts=costs;
correctRoute=route;
correctHappy=happy;
correctAveHappy=ave_happy;
return;
}
}
}
else
for(i=0;i<N;i++)
if(!visit[i]&&cost[present][i]!=0)
{
dfs(present,i,end);
route.pop_back();
costs-=cost[i][present];
happy-=happiness[i];
visit[i]=false;
}
}
...全文
250 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_42153923 2019-04-29
  • 打赏
  • 举报
回复
引用 2 楼 greex 的回复:
要做什么?能看出的就是函数太长....
是牛客网上PAT的题目,就是一个最短路问题 我用的是dfs算法。
qq_42153923 2019-04-29
  • 打赏
  • 举报
回复
引用 1 楼 单眼皮的根号3 的回复:
什么题目没说,问题也没描述,怎么帮你
说了哦 是牛客网上PAT的题目:All roads lead to rome.
greex 2019-04-29
  • 打赏
  • 举报
回复
要做什么?能看出的就是函数太长....
  • 打赏
  • 举报
回复
什么题目没说,问题也没描述,怎么帮你

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧