HDU Today-SPEA
Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
这样住了一段时间,徐总对当地的交通还是不太了解。有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格)。
徐总经常会问蹩脚的英文问路:“Can you help me?”。看着他那迷茫而又无助的眼神,热心的你能帮帮他吗?
请帮助他用最短的时间到达目的地(假设每一路公交车都只在起点站和终点站停,而且随时都会开)。
Input
第二行有徐总的所在地start,他的目的地end;
接着有n行,每行有站名s,站名e,以及从s到e的时间整数t(0<t<100)(每个地名是一个长度不超过30的字符串)。
note:一组数据中地名数不会超过150个。
如果N==-1,表示输入结束。
Output
Sample Input
Sample Output
/*
Author: 2486
Memory: 2136 KB Time: 3151 MS
Language: G++ Result: Accepted
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <cstdlib>
using namespace std;
const int maxn=170;
const int INF=100000000;
map<string,int>F;
string st,en,n1,n2;
int val,cnt,n;
struct edge {int to,cost;edge(int to,int cost):to(to),cost(cost) {}
};
vector<edge>G[maxn];
int d[maxn];
typedef pair<int,int>P;
void init() {for(int i=0; i<maxn; i++)G[i].clear();F.clear();
}
void dijkstra(int s) {priority_queue<P,vector<P>,greater<P> >que;for(int i=0;i<cnt;i++)d[i]=INF;d[s]=0;que.push(P(0,s));while(!que.empty()) {P p=que.top();que.pop();int v=p.second;if(d[v]<p.first)continue;for(int i=0; i<G[v].size(); i++) {edge e=G[v][i];if(d[e.to]>d[v]+e.cost) {d[e.to]=d[v]+e.cost;que.push(P(d[e.to],e.to));}}}
}
int main() {//freopen("D://imput.txt","r",stdin);while(~scanf("%d",&n),n!=-1) {cnt=1;init();cin>>st>>en;F[st]=cnt++;F[en]=cnt++;for(int i=0; i<n; i++) {cin>>n1>>n2>>val;if(F[n1]==0)F[n1]=cnt++;if(F[n2]==0)F[n2]=cnt++;G[F[n1]].push_back(edge(F[n2],val));G[F[n2]].push_back(edge(F[n1],val));}dijkstra(F[st]);if(d[F[en]]==INF)printf("-1\n");elseprintf("%d\n",d[F[en]]);}return 0;
}
int dis[155];
bool vis[155];
void dijkstra(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");elseprintf("%d\n",dis[thend]);
}
HDU Today-SPEA相关推荐
- HDU——1106排序(istringstream的使用、STLvector练习)
排序 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...
- hdu 5438 Ponds 拓扑排序
Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem ...
- HDU 1248 寒冰王座(全然背包:入门题)
HDU 1248 寒冰王座(全然背包:入门题) http://acm.hdu.edu.cn/showproblem.php?pid=1248 题意: 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票 ...
- hdu 1312 Red and Black 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 第二条深搜,题目并不难,但是做了我好久好久,由于一个细节,让我赌上了一个晚上的时间. 题目大意: ...
- HDU 1429 胜利大逃亡(续) (BFS+位压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) ...
- hdu 1272 小希的迷宫
Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该 ...
- HDOJ 1157 HDU 1157 Who's in the Middle ACM 1157 IN HDU
MiYu原创, 转帖请注明 : 转载自 ______________白白の屋 题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1157 题目描述: ...
- 大数加法【HDU 1002】
大数加法模板 一般的加法只要int类型的两数直接相加即可,大一点的数可以设为long long类型,而超过长整型的数则属于大数问题了,大数加法其实也比较简单,利用数组实现就可以啦: 主要思想如下: ( ...
- hdu 2665(主席树查询区间k大值)
先贴我自己写的代码做模板虽然跟原博主没什么两样.(一开始空间开的4*maxn,交到hdu上一直TLE很奇怪) #include<bits/stdc++.h> using namespace ...
- hdu 1599 find the mincost route(找无向图最小环)(floyd求最小环)
ps(我到今天才知道Floyd的核心思想是动态规划==) hdu 1599 find the mincost route(找无向图最小环) 注意!这里写成 #define data 0x3f3f3 ...
最新文章
- 轨迹规划概念总结——Dubins曲线
- Android 目录
- matlab求最大公倍数_小学数学最大公因数最小公倍数练习
- Learn Python 014: Funtions
- linux nona怎么用_Linux nano编辑器命令常用方法
- 从零开始学安全(二十三)●用PHP编写留言板
- 计算机组成原理试题,计算机组成原理试卷A卷.doc
- MsChart控件在VC++中的使用(VS2013+MFC+对话框)
- 无源蜂鸣器c语言编程,无源蜂鸣器+LED
- 【C语言】科学计数法——复习总结
- Windows Server 2016忘记密码破解方法
- 微信公众号的调试与测试
- 闲谈IPv6-为了每一粒沙,真实的代价!
- 三角函数 sinx, cosx 的泰勒展开推导及两个巧妙应用
- Pollard’s rho 算法解大数质因子问题
- 数显之家快讯:【SHIO世硕心语】小聪明毁大前程,所有人都应该深思!
- 手机影像ISP流程:AWB(1)
- 云计算--day07
- Vi编辑完如何保存退出
- 软件开发团队常见角色职责