考查点:Dijkstra
提交情况:输入处理没做好,结果调了半天。。。虽然没报错,是因为输入是字符串,所以最后接受的是空串,注意这里输入是n-1而不是n。。还有以后调试多输出中间结果,比较快。本题思路直接套dj算法,在维护几个数组增加判断情况即可
#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#define FOR(i, x, y) for(int i = x; i <= y; i++)
#define rFOR(i, x, y) for(int i = x; i >= y; i--)
#define MAX 220
#define oo 0x3f3f3f3f
using namespace std;
int G[MAX][MAX],pre[MAX],dis[MAX],vis[MAX],n,m;
int w[MAX],cost[MAX],avg[MAX],num[MAX];
map<string,int> mp;
int path[MAX];
map<int,string> pm;
int cnt=1;
int getId(string st)
{
if(mp.find(st)!=mp.end()){
return mp[st];
}else{
mp[st]=cnt;
pm[cnt]=st;
return cnt++;
}
}
void DJ(int s)
{
fill(dis,dis+MAX,oo);
fill(cost,cost+MAX,oo);
dis[s]=0,cost[s]=0;num[s]=0;path[s]=1;
FOR(i,0,n)pre[i]=-1;
FOR(i,1,n)
{
int MIN=oo,u=-1;
FOR(j,0,n-1)
{
if(vis[j]==0&&MIN>dis[j]){
MIN=dis[j];
u=j;
}
}
if(u==-1)return;
vis[u]=1;
FOR(v,0,n-1)
{
if(vis[v]==0&&G[v][u]!=oo)
{
if(dis[v]>dis[u]+G[u][v]){
dis[v]=dis[u]+G[u][v];
cost[v]=cost[u]+w[v];
pre[v]=u;
num[v]=num[u]+1;
path[v]=path[u];
}else if(dis[v]==dis[u]+G[u][v]){
path[v]+=path[u];
if(cost[v]<cost[u]+w[v]){
cost[v]=cost[u]+w[v];
pre[v]=u;
num[v]=num[u]+1;
}else if(cost[v]==cost[u]+w[v]){
if(num[v]>num[u]+1){
num[v]=num[u]+1;
pre[v]=u;
}
}
}
}
}
}
}
void DFS(int s,int d)
{
if(d==s){
cout<<pm[s];return;
}
DFS(s,pre[d]);
cout<<"->"<<pm[d];
}
int main()
{
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif // LOCAL
int x;
fill(G[0],G[0]+MAX*MAX,oo);
string st;
cin>>n>>m>>st;
mp[st]=0;pm[0]=st;
FOR(i,1,n-1)
{
cin>>st;
int id=getId(st);
cin>>w[id];
}
string s1,s2;
FOR(i,1,m)
{
cin>>s1>>s2>>x;
int id1=mp[s1],id2=mp[s2];
G[id1][id2]=G[id2][id1]=x;
}
DJ(0);
int id=getId("ROM");
printf("%d %d %d %d\n",path[id],dis[id],cost[id],cost[id]/num[id]);
DFS(0,id);
return 0;
}