本来是拿来复习一下map的,没想搞了半天,一直wa,最后发现预处理没有处理到所有的点

就是个最短路

Sample Input

6
xiasha westlake
xiasha station 60
xiasha ShoppingCenterofHangZhou 30
station westlake 20
ShoppingCenterofHangZhou supermarket 10
xiasha supermarket 50
supermarket westlake 10
-1 Sample Output
50

Hint:
The best route is:
xiasha->ShoppingCenterofHangZhou->supermarket->westlake 虽然偶尔会迷路,但是因为有了你的帮助
**和**从此还是过上了幸福的生活。 ――全剧终――
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
const int maxint=;
//printf("----\n");
using namespace std;
int m,t;
int tot=;
map<string,int> st;
int c[][],dist[];
void dijkstra(int u,int n)
{
//printf("%d\n",n);
bool vis[];
for(int i=;i<=n;i++)
{
vis[i]=;
dist[i]=c[u][i];
}
vis[u]=;
for(int i=;i<=n;i++)
{
int temp=maxint;
int k=u;
for(int j=;j<=n;j++)
{
if(!vis[j]&&dist[j]<temp)
{
temp=dist[j];
k=j;
}
}
if(temp==maxint) break;
vis[k]=;
for(int j=;j<=n;j++)
{
if(!vis[j]&&c[k][j]<maxint)
{
if(dist[k]+c[k][j]<dist[j])
dist[j]=dist[k]+c[k][j];
}
}
}
}
int main()
{
int i,j,k,n;
//freopen("1.in","r",stdin);
while(scanf("%d",&n)!=EOF)
{
bool flag=;
if(n==-) break;
st.clear();
char s[],e[];
scanf("%s%s",s,e);
if(strcmp(s,e)==)
{
flag=;
}
st[s]=;
st[e]=;
tot=;
for(i=;i<=;i++)
{
for(j=;j<=;j++) c[i][j]=c[j][i]=((i==j)?:maxint);
}
for(i=;i<=;i++)
{
dist[i]=maxint;
}
for(i=;i<=n;i++)
{
int w;
scanf("%s%s%d",s,e,&w);
if(!st[s]) st[s]=tot++;
if(!st[e]) st[e]=tot++;
c[st[s]][st[e]]=c[st[e]][st[s]]=w;
}
dijkstra(,tot);
if(flag) printf("0\n");
else if(dist[]==maxint) printf("-1\n");
else
printf("%d\n",dist[]);
}
return ;
}

2015-04-24:这个是kuangbin模板的,检测了一下,算法里的起点是从0开始的,那个优化代码不知道怎么就wa了,等刷到kuang大神的最短路再看吧

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const double eps=1e-;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=;
int n,m,tt;
#define typec int
const typec INF=;//防止后面溢出,这个不能太大
int c[MAXN][MAXN],tot=,dist[];
bool vis[MAXN];
int pre[MAXN];
void Dijkstra(typec cost[][MAXN],typec lowcost[],int n,int beg)
{
for(int i=;i<=n;i++)
{
lowcost[i]=INF;vis[i]=false;pre[i]=-;
}
lowcost[beg]=;
for(int j=;j<n;j++)
{
int k=-;
int Min=INF;
for(int i=;i<=n;i++)
if(!vis[i]&&lowcost[i]<Min)
{
Min=lowcost[i];
k=i;
}
if(k==-)break;
vis[k]=true;
for(int i=;i<=n;i++)
if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i])
{
lowcost[i]=lowcost[k]+cost[k][i];
pre[i]=k;
}
}
}
map<string,int> st;
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
while(scanf("%d",&n)!=EOF)
{
bool flag=;
if(n==-) break;
st.clear();
char s[],e[];
scanf("%s%s",s,e);
if(strcmp(s,e)==)
{
flag=;
}
st[s]=;
st[e]=;
tot=;
for(i=;i<=;i++)
{
for(j=;j<=;j++) c[i][j]=c[j][i]=((i==j)?:INF);
}
for(i=;i<=;i++)
{
dist[i]=INF;
}
for(i=;i<=n;i++)
{
int w;
scanf("%s%s%d",s,e,&w);
if(!st[s]) st[s]=tot++;
if(!st[e]) st[e]=tot++;
c[st[s]][st[e]]=c[st[e]][st[s]]=w;
}
Dijkstra(c,dist,tot,);
if(flag) printf("0\n");
else if(dist[]==INF) printf("-1\n");
else
printf("%d\n",dist[]);
}
}

hdu 2112 最短路的更多相关文章

  1. hdu 2112 (最短路+map)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others)  ...

  2. ACM: HDU 2544 最短路-Dijkstra算法

    HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  3. UESTC 30 &&HDU 2544最短路【Floyd求解裸题】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. hdu 5521 最短路

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  5. HDU - 2544最短路 (dijkstra算法)

    HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...

  6. F - JDG HDU - 2112 (最短路)&& E - IGNB HDU - 1242 (dfs)

    经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬 ...

  7. 【hdu 2112】 HDU Today ( 最短路 Dijkstra)(map)

    http://acm.hdu.edu.cn/showproblem.php?pid=2112 这道题给了一个将字符串与int对应的思路,就是使用map 这道题答案对了,但是没有AC,我也不知道为什么. ...

  8. hdu 2112 HDU Today (最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目大意:给出起点和终点,然后算出最短的路. 不过有好多细节要注意: (1)起始点和终止点相等的 ...

  9. HDU 2112 HDU Today 最短路

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

随机推荐

  1. trace spring

    package xx.com.aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotati ...

  2. Linux 多线程编程—使用条件变量实现循环打印

    编写一个程序,开启3个线程,这3个线程的ID分别为A.B.C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示:如:ABCABC….依次递推. 使用条件变量来实现: #inc ...

  3. 二、springboot配置

    一.启动类 在包根目录下添加启动类,必须包含main方法,再添加Spring Boot启动方法: SpringApplication.run(SampleController.class, args) ...

  4. 用sklearn计算卡方检验P值

    情形: 1. 对于一批分类变量,我们通常要评价两两之间的相关程度. 2. 因变量是分类变量,衡量其他分类变量和因变量的相关性高低. 来源:https://blog.csdn.net/snowdropt ...

  5. 【鬼脸原创】JQuery获取元素的方法总结

    目录 一.说明 二.获取本身 三.获取同级元素 四.获取父级元素 五.获取子级元素 一.说明   获取元素的方法分为两种:jQuery选择器.jQuery遍历函数. 做个总结,巩固下知识. 二.获取本 ...

  6. java基础18 String字符串和Object类(以及“equals” 和 “==”的解析)

    一.String字符串 问:笔试题:new String("abc")创建了几个对象?答:两个对象,一个对象是 位于堆内存,一个对象位于字符串常量池 class Demo17 { ...

  7. SQL Server和Access数据读写

    1.查询Access中数据的方法: select * from OpenRowSet('microsoft.jet.oledb.4.0',';database=c:/db2.mdb','select ...

  8. Python输入/输出

    1.在python2.x中raw_input( )和input( ),两个函数都存在,其中区别为 raw_input( )---将所有输入作为字符串看待,返回字符串类型 input( )-----只能 ...

  9. Linux同步互斥(Peterson算法,生产者消费者模型)

    同步 两个或两个以上随时间变化的量在变化过程中保持一定的相对关系. 互斥 对一组并发进程,一次只有一个进程能够访问一个给定的资源或执行一个给定的功能. 互斥技术可以用于解决诸如资源争用之类的冲突,还可 ...

  10. WORDPRESS登录后台半天都无法访问或者是访问慢的解决方法

    wordpress登录后台如果打开速度慢,一般分为两部分,第一部分是php虚拟主机的原因,其中主机的原因,又分为很多种情况.第二部分就是WordPress程序本身的问题.这里无忧主机小编主要是讲第二部 ...