7-14 垃圾箱分布
这题写了老半天,发现写的有问题,但是没找到问题在哪,我又重写了。
对每个垃圾箱跑堆优化的dijkstra,但是最后测样例的时候发现精度不对,交上去却是对的,这题有点坑。
#include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e3+50;
vector<pair<int,int> > g[maxn];
int vis[maxn];
int d[maxn];
int n,m,k,ds,tmp;
struct Node {
int id;
double sum;
int dis;
Node(int i,double s,int d):id(i),sum(s),dis(d) {}
};
vector<Node> ans;
bool cmp(Node& a,Node& b) {
if (a.dis!=b.dis) {
return a.dis>b.dis;
}
else if (a.sum!=b.sum) {
return a.sum<b.sum;
}
return a.id<b.id;
}
struct Dode {
int id;
int dis;
Dode(int i,int d):id(i),dis(d){}
bool operator < (const Dode& a) const {
return dis>a.dis;
}
};
void dijkstra(int s) {
memset(vis,0,sizeof(vis));
memset(d,INF,sizeof(d));
d[s]=0;
priority_queue<Dode> pq;
pq.push(Dode(s,0));
while (!pq.empty()) {
Dode f=pq.top();
pq.pop();
if (vis[f.id]) {
continue;
}
vis[f.id]=1;
// printf("%d\n",f.id);
for (auto p:g[f.id]) {
int next=p.first;
int cost=p.second;
// printf("%d %d\n",next,cost);
if (!vis[next]&&f.dis+cost<d[next]) {
d[next]=f.dis+cost;
pq.push(Dode(next,d[next]));
}
}
}
}
int judge(int s) {
// printf("s: %d\n",s);
double sum=0;
int minlen=INF;
for (int i=1;i<=n;i++) {
if (d[i]>ds) {
return 0;
}
// printf("%d ",d[i]);
sum+=d[i];
minlen=min(d[i],minlen);
}
// printf("\n");
ans.push_back(Node(s-1000,sum,minlen));
return 1;
}
int main()
{
scanf("%d%d%d%d",&n,&m,&k,&ds);
char p1[100],p2[100];
while (k--) {
scanf("%s",p1);
scanf("%s",p2);
scanf("%d",&tmp);
int s=0,e=0;
if (p1[0]=='G') {
s=1000+atoi(p1+1);
}
else {
s=atoi(p1);
}
if (p2[0]=='G') {
e=1000+atoi(p2+1);
}
else {
e=atoi(p2);
}
g[s].push_back(make_pair(e,tmp));
g[e].push_back(make_pair(s,tmp));
}
int cnt=0;
for (int i=1001;i<=1000+m;i++) {
dijkstra(i);
cnt+=judge(i);
}
if (!cnt) {
printf("No Solution\n");
return 0;
}
sort(ans.begin(),ans.end(),cmp);
printf("G%d\n",ans[0].id);
printf("%.1f %.1f\n",(double)ans[0].dis,ans[0].sum/(double)n);
return 0;
}