</pre><p class="p1"><pre name="code" class="cpp">#include <iostream>
#include <cstring>
#include <string>
#include <map>
#include <algorithm>
#include <stack>
#include <vector>
using namespace std;
#define MAX 201
#define INF 0x7fffffff
struct Node
{
string name;
int happy;
};
vector<int>ha;
int edge[MAX][MAX];
int dist[MAX];
int hap[MAX];
int visited[MAX];
int pre[MAX];
int n,k;
int happ[MAX][MAX]; Node nodes[MAX];
void dijkstra(int s,int d)
{
visited[s]=1;
for(int i=0;i<n;i++)
{
dist[i]=edge[s][i];
hap[i]=happ[s][i];
if(dist[i]<INF)
pre[i]=s;
else
pre[i]=0;
}
dist[s]=0;
int u=s;
for(int i=1;i<n;i++)
{
int mindist=INF;
int bighappy=0;
for(int j=0;j<n;j++)
{
if((!visited[j])&&((dist[j]<mindist)||(dist[j]==mindist&&hap[j]>bighappy)))
{
mindist=dist[j];
bighappy=hap[j];
u=j;
}
}
visited[u]=1;
for(int j=0;j<n;j++)
{
if((!visited[j])&&(edge[u][j]<INF))
{
int newdist=dist[u]+edge[u][j];
int newhap=hap[u]+happ[u][j];
if(newdist<dist[j]||(newdist==dist[j]&&newhap>hap[j]))
{
dist[j]=newdist;
hap[j]=newhap;
pre[j]=u;
}
}
}
}
}
int len;
int cnt=0;
int ha_sum=0;
void dfs(int s,int d,int leng)
{
visited[s]=1;
for(int i=0;i<n;i++)
{
if(!visited[i]&&edge[s][i]<INF)
{
len+=edge[s][i];
ha_sum+=nodes[i].happy;
if(i==d)
{
break;
}
else
dfs(i, d,leng);
}
}
if(len==leng)
{len=0;
cnt++;
ha.push_back(ha_sum);
ha_sum=0;
}
}
int main()
{
cin>>n>>k;
string tmp;
cin>>tmp;
nodes[0].name=tmp;
nodes[0].happy=0;
for(int i=1;i<n;i++)
{
cin>>nodes[i].name>>nodes[i].happy;
}
map<string,int> m;
map<int,string>city;
for(int i=0;i<n;i++)
{
m.insert(make_pair(nodes[i].name,i));
city.insert(make_pair(i, nodes[i].name));
}
for(int i=0;i<k;i++)
for(int j=0;j<k;j++)
edge[i][j]=INF;
for(int i=0;i<k;i++)
{
string a,b;
int c;
cin>>a>>b>>c;
map<string,int>::iterator iter=m.find(a),iter1=m.find(b);
edge[iter->second][iter1->second]=c;
edge[iter1->second][iter->second]=c;
happ[iter->second][iter1->second]=nodes[iter->second].happy+nodes[iter1->second].happy;
happ[iter1->second][iter->second]=nodes[iter->second].happy+nodes[iter1->second].happy;
}
string rom="ROM";
map<string,int>::iterator it=m.find(tmp),it2=m.find(rom);
memset(visited,0,sizeof(visited));
dijkstra(it->second,it2->second);
memset(visited,0,sizeof(visited));
len=0;
dfs(it->second,it2->second,dist[it2->second]);
cout<<cnt<<" "<<dist[it2->second]<<" ";
stack<int>ss,ss2;
ss.push(it2->second);
ss2.push(it2->second);
int a=it2->second;
while(a!=it->second)
{
a=pre[a];
ss.push(a);
ss2.push(a);
}
int sum=0;
int l=ss.size();
while(!ss.empty())
{
sum+=nodes[ss.top()].happy;
ss.pop();
}
cout<<sum<<" ";
int ave=sum/(l-1);
cout<<ave<<endl;
while(!ss2.empty())
{
map<int,string>::iterator it=city.find(ss2.top());
cout<<it->second;
if(ss2.size()!=1)
cout<<"->";
ss2.pop();
}
}
PAT 未完成
最新推荐文章于 2022-04-12 18:37:25 发布