hdu2112 HDU Today

题目链接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=10000+6;
const int E=200001;
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=0;
    memset(head,-1,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=1;i<=n;i++)
    {
        d[i]=INF;
        cnt[i]=0;
        vis[i]=1;
    }
    while(!que.empty())
        que.pop();
    que.push(src);
    vis[src]=0;
    d[src]=0;
    ++cnt[src];

    while(!que.empty())
    {
        u=que.front();
        que.pop();
        vis[u]=1;
        for(i=head[u];i!=-1;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]=0;
                }
            }
        }
    }
    if(d[n]==INF)
        return -1;

    return d[n];
}


int main()
{
    int n;

    while(scanf("%d",&n))
    {
        ok=0;
        int k=2;
        if(n==-1) 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(1,n);
        }
        if(ok) printf("0
");
        else if(d[2]==INF)  printf("-1
");
        else printf("%d
",d[2]);
    }
    return 0;
}