#include<iostream>
#include<cstring>
#include<iomanip>
#include<algorithm>
#include<vector>
#define INF 0x7ffffff
#define NUM 1000+10+2
using namespace std;
int n,m,k,ds;
int map[NUM][NUM]={0};
int visited[NUM];
int dist[NUM];
struct node{
int index; //gas index
int mm; //距离house的最小距离
int sum; //所有house的距离和
node(int index,int mm,int sum):index(index),mm(mm),sum(sum){} //注意这里的写法,
///之前用结构体数组的时候第一个测试点不能通过,可能是因为在存入数组的时候有问题
};
//node gas[11];//结构体数组只能定义在这里,不要写在main函数中,不然会报错
bool cmp(const struct node &a,const struct node& b)
{
if(a.mm>b.mm)return true;
else if(a.mm==b.mm)
{
if(a.sum<b.sum)return true;
else if(a.sum==b.sum)return a.index<b.index;
}
return false;
}
int strtonum(char *str,int n)
{
int num;
if(str[0]=='G')num=atoi(str+1)+n;
else num=atoi(str);
return num;
}
/*void init()
{
int i,j;
for(i=1;i<NUM;i++)
{
for(j=1;j<NUM;j++)
{
map[i][j]=0;
}
}
}*/
void dijkstra(int v)
{
int i,j,k=0;
int min;
for(i=1;i<=n+m;i++)
{
visited[i]=0;//注意在这里记得初始化为0
if(map[v][i]>0&&i!=v)
{
dist[i]=map[v][i];
}
else
{
dist[i]=INF;
}
}
visited[v]=1;
for(i=1;i<=n+m;i++)
{
min=INF;
for(j=1;j<=n+m;j++)
{
if(min>dist[j]&&v!=j&&!visited[j])
{
min=dist[j];
k=j;
}
}
visited[k]=1;
//更新
for(j=1;j<=n+m;j++)
{
if(dist[j]>dist[k]+map[k][j]&&map[k][j]>0&&!visited[j])
{
dist[j]=dist[k]+map[k][j];
}
}
}
}
int main()
{
int i,j,x,y;
int min;
int sum;
vector<node>vec;
char s1[10],s2[10];
int dis;
cin>>n>>m>>k>>ds;
//init();
for(i=1;i<=k;i++)
{
cin>>s1>>s2>>dis;
x=strtonum(s1,n);
y=strtonum(s2,n);
map[x][y]=map[y][x]=dis;
}
bool flag;
// 对于m个源点
//int count=0;
for(i=1;i<=m;i++)
{
flag=true;
dijkstra(i+n);
min=INF;
sum=0;
for(j=1;j<=n;j++)
{
if(dist[j]>ds)
{
flag=false;
break;
}
if(min>dist[j])min=dist[j];
sum+=dist[j];
}
if(!flag)continue;
vec.push_back(node(i,min,sum));
/*gas[i].index=i;
gas[i].mm=min;
gas[i].sum=sum;
count++;*/
//这里如果用结构体数组,那么数组中有的就是空的,排序的时候可能会出现问题,结构体数组怎么初始化是一个问题
//还是用动态数组比较好
}
if(vec.size())
{
//sort(gas+1,gas+m,cmp);
sort(vec.begin(),vec.end(),cmp);
cout<<"G"<<vec[0].index<<endl;
double aver=(vec[0].sum*100/100.0)/n;
//cout<<aver<<endl;
double m=vec[0].mm*100/100.0;
cout<<setiosflags(ios::fixed)<<setprecision(1)<<m<<' ';
cout<<setprecision(1)<<aver<<endl;
}
else
{
cout<<"No Solution"<<endl;
}
system("pause");
return 0;
}
#include<cstring>
#include<iomanip>
#include<algorithm>
#include<vector>
#define INF 0x7ffffff
#define NUM 1000+10+2
using namespace std;
int n,m,k,ds;
int map[NUM][NUM]={0};
int visited[NUM];
int dist[NUM];
struct node{
int index; //gas index
int mm; //距离house的最小距离
int sum; //所有house的距离和
node(int index,int mm,int sum):index(index),mm(mm),sum(sum){} //注意这里的写法,
///之前用结构体数组的时候第一个测试点不能通过,可能是因为在存入数组的时候有问题
};
//node gas[11];//结构体数组只能定义在这里,不要写在main函数中,不然会报错
bool cmp(const struct node &a,const struct node& b)
{
if(a.mm>b.mm)return true;
else if(a.mm==b.mm)
{
if(a.sum<b.sum)return true;
else if(a.sum==b.sum)return a.index<b.index;
}
return false;
}
int strtonum(char *str,int n)
{
int num;
if(str[0]=='G')num=atoi(str+1)+n;
else num=atoi(str);
return num;
}
/*void init()
{
int i,j;
for(i=1;i<NUM;i++)
{
for(j=1;j<NUM;j++)
{
map[i][j]=0;
}
}
}*/
void dijkstra(int v)
{
int i,j,k=0;
int min;
for(i=1;i<=n+m;i++)
{
visited[i]=0;//注意在这里记得初始化为0
if(map[v][i]>0&&i!=v)
{
dist[i]=map[v][i];
}
else
{
dist[i]=INF;
}
}
visited[v]=1;
for(i=1;i<=n+m;i++)
{
min=INF;
for(j=1;j<=n+m;j++)
{
if(min>dist[j]&&v!=j&&!visited[j])
{
min=dist[j];
k=j;
}
}
visited[k]=1;
//更新
for(j=1;j<=n+m;j++)
{
if(dist[j]>dist[k]+map[k][j]&&map[k][j]>0&&!visited[j])
{
dist[j]=dist[k]+map[k][j];
}
}
}
}
int main()
{
int i,j,x,y;
int min;
int sum;
vector<node>vec;
char s1[10],s2[10];
int dis;
cin>>n>>m>>k>>ds;
//init();
for(i=1;i<=k;i++)
{
cin>>s1>>s2>>dis;
x=strtonum(s1,n);
y=strtonum(s2,n);
map[x][y]=map[y][x]=dis;
}
bool flag;
// 对于m个源点
//int count=0;
for(i=1;i<=m;i++)
{
flag=true;
dijkstra(i+n);
min=INF;
sum=0;
for(j=1;j<=n;j++)
{
if(dist[j]>ds)
{
flag=false;
break;
}
if(min>dist[j])min=dist[j];
sum+=dist[j];
}
if(!flag)continue;
vec.push_back(node(i,min,sum));
/*gas[i].index=i;
gas[i].mm=min;
gas[i].sum=sum;
count++;*/
//这里如果用结构体数组,那么数组中有的就是空的,排序的时候可能会出现问题,结构体数组怎么初始化是一个问题
//还是用动态数组比较好
}
if(vec.size())
{
//sort(gas+1,gas+m,cmp);
sort(vec.begin(),vec.end(),cmp);
cout<<"G"<<vec[0].index<<endl;
double aver=(vec[0].sum*100/100.0)/n;
//cout<<aver<<endl;
double m=vec[0].mm*100/100.0;
cout<<setiosflags(ios::fixed)<<setprecision(1)<<m<<' ';
cout<<setprecision(1)<<aver<<endl;
}
else
{
cout<<"No Solution"<<endl;
}
system("pause");
return 0;
}