这是个非常忧伤的题。
且不说 他的样例输出 欺骗了我。
害的我去输出 路径。
这特么居然是个 无向图…………
无向图就算了。
C++ AC。。。。
G++超时是要闹哪样!
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<queue>
#include<map>
#include<iostream>
#define INF 0xfffffff
using namespace std;
int m;
struct lx
{
int v,t;
};
vector<lx>g[155];
map<string,int>str;
int dis[155];
bool vis[155];
void SPFA(int start,int thend)
{
for(int i=0;i<155;i++)
dis[i]=INF,vis[i]=0;
dis[start]=0,vis[start]=1;
queue<int>q;
q.push(start);
while(!q.empty())
{
int u=q.front();q.pop();
vis[u]=0;
for(int j=0;j<g[u].size();j++)
{
int v=g[u][j].v;
int t=g[u][j].t;
if(dis[v]>dis[u]+t)
{
dis[v]=dis[u]+t;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
if(dis[thend]>=INF)
puts("-1");
else
printf("%d\n",dis[thend]);
}
int main()
{
while(scanf("%d",&m)!=EOF)
{
if(m==-1)return 0;
str.clear();
for(int i=0;i<155;i++)
g[i].clear();
int cot=1;
int u,v,t;
int start,thend;
string s1,s2;
cin>>s1>>s2;
if(str[s1]==0)
str[s1]=cot++;
start=str[s1];
if(str[s2]==0)
str[s2]=cot++;
thend=str[s2];
while(m--)
{
cin>>s1>>s2>>t;
if(str[s1]==0)
str[s1]=cot++;
u=str[s1];
if(str[s2]==0)
str[s2]=cot++;
v=str[s2];
lx now;
now.t=t;
now.v=v;
g[u].push_back(now);
now.v=u;
g[v].push_back(now);
}
SPFA(start,thend);
}
}