题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112
题目分类:SPFA算法+map容器
错误点:红色标记部分
代码:
#include<bits/stdc++.h> using namespace std; map<string ,int> mymap; string s1,s2,s3,s4; int ok1,ok2;
int dis; const int INF=0x3f3f3f3f;
const int V=+;
const int E=;
int pnt[E],cost[E],nxt[E];
int e,head[V],d[V];
int temp[V][V];
bool vis[V];
int cnt[V];
int x[V];
int y[V];
int ans;
int ver;
bool ok; queue<int> que; void init()
{
e=;
memset(head,-,sizeof(head));
} inline void addedge(int u,int v,int c)
{
pnt[e]=v;
cost[e]=c;
nxt[e]=head[u];
head[u]=e++;
} int SPFA(int src,int n)
{
int i,u,v;
for(i=;i<=n;i++)
{
d[i]=INF;
cnt[i]=;
vis[i]=;
}
while(!que.empty())
que.pop();
que.push(src);
vis[src]=;
d[src]=;
++cnt[src]; while(!que.empty())
{
u=que.front();
que.pop();
vis[u]=;
for(i=head[u];i!=-;i=nxt[i])
{
v=pnt[i];
if(d[v]>d[u]+cost[i])
{
d[v]=d[u]+cost[i];
if(vis[v])
{
que.push(v);
vis[v]=;
}
}
}
}
if(d[n]==INF)
return -; return d[n];
} int main()
{
int n; while(scanf("%d",&n))
{
ok=;
int k=;
if(n==-) break;
cin>>s1>>s2;
int m = n;
init();
mymap.clear();
mymap.insert(map<string,int>::value_type(s1,1));
mymap.insert(map<string,int>::value_type(s2,2));
while(m--)
{ cin>>s3>>s4>>dis;
if(mymap.find(s3) == mymap.end())
{
k++;
mymap.insert(map<string,int>::value_type(s3,k));
}
if(mymap.find(s4) == mymap.end())
{
k++;
mymap.insert(map<string,int>::value_type(s4,k));
}
addedge(mymap[s3],mymap[s4],dis);
addedge(mymap[s4],mymap[s3],dis);
//cout<<mymap[s3]<<endl;
} if(s1==s2)
{
ok=1;
}
else
{
SPFA(,n);
}
if(ok) printf("0\n");
else if(d[2]==INF) printf("-1\n");
else printf("%d\n",d[]);
}
return ;
}