链接:https://www.nowcoder.com/questionTerminal/aba54c5142454a4f830b3eb93dd71d3a?toCommentId=15078641&ran=475
来源:牛客网
有重复边😡🤬🤬🤬🤬🤬🤬,取最小的边,浪费了一天md
#include<bits/stdc++.h>
using namespace std;
const int Max=1020;
const int INF=INT_MAX;
int n,m,k,d;
int dis[Max],G[Max][Max];
bool visit[Max];
void Dijistra(int s) {
memset(visit,0,sizeof(visit));
fill(dis,dis+Max,INF);
dis[s]=0;
for(int i=0; i<m+n; i++) {
int u=-1,Min=INF;
for(int j=1; j<=m+n; j++) {
if(dis[j]<Min&&!visit[j]) {
Min=dis[j];
u=j;
}
}
if(u==-1) return ;
visit[u]=1;
for(int v=1; v<=m+n; v++) {
if(!visit[v]&&G[u][v]!=INF) {
if(dis[v]>dis[u]+G[u][v]) {
dis[v]=dis[u]+G[u][v];
}
}
}
}
}
int getid(char* s) {
int i=0,l=strlen(s),id=0;
while(i<l) {
if(s[i]!='G')
id=id*10+(s[i]-'0');
i++;
}
if(s[0]=='G') return n+id;
else return id;
}
int main() {
scanf("%d %d %d %d",&n,&m,&k,&d);
fill(G[0],G[0]+Max*Max,INF);
for(int i=0; i<k; i++) {
char s1[5],s2[5];
int c;
scanf("%s %s",s1,s2);
int from,to;
from=getid(s1);
to=getid(s2);
scanf("%d",&c);
G[to][from]=G[from][to]=min(c,G[to][from]);
}
int ansid=-1;
double ansdis=-1,ansavg=INF;
for(int i=n+1; i<=m+n; i++) {
double mindis=INF,minavg=0;
Dijistra(i);
if(*max_element(dis+1,dis+n+1)>d) continue;
mindis=*min_element(dis+1,dis+n+1);
minavg=accumulate(dis+1,dis+n+1,0)*1.0/n;
if(ansdis<mindis) {
ansid=i;
ansdis=mindis;
ansavg=minavg;
} else if(ansdis==mindis&&ansavg>minavg) {
ansid=i;
ansavg=minavg;
}
}
if(ansid==-1) printf("No Solution\n");
else {
printf("G%d\n",ansid-n);
printf("%.1f %.1f\n",ansdis,ansavg);
}
return 0;
}
Gas Station
最新推荐文章于 2024-08-28 22:49:00 发布