题目链接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 ;
}

hdu2112 HDU Today的更多相关文章

  1. HDU2112 HDU Today 最短路+字符串哈希

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. hdu2112(HDU Today 简单最短路)

    Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD ...

  3. Hdu-2112 HDU Today (单源多点最短路——Dijsktra算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目大意:给你N个公交车站,起点,终点,各站之间的距离,求起点到终点之间的最短距离.(起点终点相 ...

  4. hdu-2112 HDU Today---dijkstra+标号

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2112. 题目大意: 求起点到终点的最短路 解题思路: 对地名进行编号即可 然后直接dijkstra算 ...

  5. hdu2112 HDU Today 基础最短路

    这题的关键是把车站的名字转化为点的编号.我用的是map.声明一个map<string,int> st,然后按照字符串出现的次序给st赋值.例如:st[s1]=2;代表这字符串s1出现的次序 ...

  6. HDU Tody HDU2112

    不想用floyd了 也不一定适合  floyd只能处理小数据 dijkstra算法 wa了很久   一个是dijkstra里面的u   导致RE了无数次   下标溢出 还有就是注意细节  当起点和终点 ...

  7. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  9. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

随机推荐

  1. Introduction to the Build Lifecycle

    Introduction to the Build Lifecycle Table Of Contents Build Lifecycle Basics Setting Up Your Project ...

  2. C# - 接口的继承

    代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  3. C语言,C++,static

    术语static有着不寻常的历史.起初,在C中引入关键字static是为了表示退出一个块后仍然存在的局部变量.随后,static在C中有了第二种含义:用来表示不能被其它文件访问的全局变量和函数.为了避 ...

  4. TPersistent的三个用途(读写DFM文件,Assign,RTTI),最主要还是第三个用途

    不是什么类对象都需要RTTI,如果把它放在TObject,除了增加可执行文件的大小以及运行内存空间以外,没什么好处.

  5. anglehack参赛总结

    自已不足的方面: 自已和伙伴是带着idea去的,但是没有带有很大的热情激励和吸引在场的hacker加入团队,一定要找最优秀的人加入团队,事实上我是有这方面识人认人的能力的,24h,5-6个人的优秀团队 ...

  6. Androidclient与服务端(jsp)之间json的传输与解析【附效果图附源代码】

    近期有个项目须要用到json的传输,之前不是太了解,在网上找了些相关资料,写了一个小小的demo,能够实现基本功能:androidclient发送json到服务端,服务端使用jsp接收,解析后以jso ...

  7. chrome你这是入侵OSX了么?

    今天突然发现安装chrome的应用后,会在Dock中出现如下图标,点击即可启动chrome相关应用,很是方便.

  8. 不包含SDK头文件, 补全API定义

    /// @file main.cpp /// @brief 不包含SDK头文件, 补全API定义 #ifdef __cplusplus extern "C" { #endif /* ...

  9. RMAN 备份

    backup database; --备份整库 backup database format '\xxxxxx\xxx_%U'; --备份整库到指定路劲 backup tablespace users ...

  10. ASP.NET - cookie

    下面是写cookie  HttpCookie cookie = new HttpCookie("Info");//定义cookie对象以及名为Info的项  DateTime dt ...