#include<bits/stdc++.h>
using namespace std;
int main()
{
double a=1.15;
printf("%.15lf\n",a);
printf("%.16lf\n",a);
printf("%.1lf\n",a);
}
输出:
1.150000000000000
1.1499999999999999
1.1
AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N=1050;
int n1,n2,m,range;
vector<PII> g[N];
int dist[N];
bool st[N];
int ans1;
int ans2=-1000;
double ans3=1e18;
void dij(int s)
{
memset(dist,0x3f,sizeof dist);
dist[s]=0;
queue<int> q;
q.push(s);
st[s]=true;
while(q.size())
{
int x=q.front();q.pop();
st[x]=false;
for(auto edge: g[x])
{
int v=edge.first,d=edge.second;
if(dist[v]>dist[x]+d)
{
dist[v]=dist[x]+d;
if(!st[v]) st[v]=true,q.push(v);
}
}
}
long long sum=0;
int minv=INT_MAX;
for(int i=1;i<=n1;i++)
{
// cout<<dist[i]<<endl;
if(dist[i]>range) return;
sum+=dist[i];
minv=min(minv,dist[i]);
}
double ave=(double)sum/n1;
if(ans2<minv)
{
ans2=minv;
ans1=s;
ans3=ave;
}else if(ans2==minv&&ans3>ave) ans1=s,ans3=ave;
}
int main()
{
cin>>n1>>n2>>m>>range;
while(m--)
{
string x,y;
int z,a,b;
cin>>x>>y>>z;
if(x[0]=='G') a=1000+stoi(x.substr(1));
else a=stoi(x);
if(y[0]=='G') b=1000+stoi(y.substr(1));
else b=stoi(y);
g[a].push_back({b,z});
g[b].push_back({a,z});
}
for(int i=1;i<=n2;i++)
{
dij(1000+i);
}
if(!ans1) puts("No Solution");
else printf("G%d\n%.1lf %.1lf",ans1-1000,(double)ans2,ans3+1e-8);
return 0;
}
想问下第一个代码的精度那里保留16位为什么反而不准了呢