kuangbin_ShortPath A (POJ 2387)
最短路模板题 但是其实很费时间 因为要看明白dij floyd 以及 dij优化 spfa优化 交了三次 大概是理解了
不过涉及到priority_queue的重载运算符问题 以后要在C++里面好好看看 现在不理解
Dijkstra ver:
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#define INF 0x3F3F3F3F
using namespace std;
typedef pair<int, int> pii; int size, head[], point[], next[], val[];
int dis[], t, n; void add(int from, int to, int value)
{
point[size] = to;
next[size] = head[from];
val[size] = value;
head[from] = size++;
} struct cmp{
bool operator () (pii a, pii b){
return a.first > b.first;
}
}; void dijkstra(int s)
{
memset(dis, 0x3F, sizeof dis);
priority_queue<pii, vector<pii>, cmp> q;
q.push(make_pair(, s));
dis[s] = ;
while(!q.empty()){
pii u = q.top();
q.pop();
if(u.first > dis[u.second]) continue;
for(int i = head[u.second]; ~i; i = next[i]){
int j = point[i];
if(dis[j] > u.first + val[i]){
dis[j] = u.first + val[i];
q.push(make_pair(dis[j], j));
}
}
}
} int main()
{
while(~scanf("%d%d", &t, &n)){
size = ;
memset(head, -, sizeof head);
for(int i = ; i <= t; i++){
int from, to, value;
scanf("%d%d%d", &from, &to, &value);
add(from, to, value);
add(to, from, value);
}
dijkstra();
printf("%d\n", dis[n]);
}
return ;
}
Spfa ver:
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#define INF 0x3F3F3F3F
using namespace std; int head[], point[], next[], val[], size; void add(int from, int to, int value)
{
point[size] = to;
val[size] = value;
next[size] = head[from];
head[from] = size++; point[size] = from;
val[size] = value;
next[size] = head[to];
head[to] = size++;
} void spfa(int s, int t)
{
int dis[];
bool vis[];
memset(dis, 0x3f, sizeof dis);
memset(vis, false, sizeof vis);
queue<int> q;
dis[s] = ;vis[s] = true;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; ~i; i = next[i]){
int j = point[i];
if(dis[j] > dis[u] + val[i]){
dis[j] = dis[u] + val[i];
if(!vis[j]){
q.push(j);
vis[j] = true;
}
}
}
}
printf("%d\n", dis[t]);
} int main()
{
int t, n;
memset(head, -, sizeof head);
scanf("%d%d", &t, &n);
while(t--){
int a, b, value;
scanf("%d%d%d", &a, &b, &value);
add(a, b, value);
}
spfa(, n);
return ;
}
kuangbin_ShortPath A (POJ 2387)的更多相关文章
- 链式前向星版DIjistra POJ 2387
链式前向星 在做图论题的时候,偶然碰到了一个数据量很大的题目,用vector的邻接表直接超时,上网查了一下发现这道题数据很大,vector可定会超的,不会指针链表的我找到了链式前向星这个好东西,接下来 ...
- hdu 2544 hdu 1874 poj 2387 Dijkstra 模板题
hdu 2544 求点1到点n的最短路 无向图 Sample Input2 1 //结点数 边数1 2 3 //u v w3 31 2 52 3 53 1 20 0 Sample Output32 ...
- POJ 2387 Til the Cows Come Home (图论,最短路径)
POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)
题目连接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...
- poj 2387 Til the Cows Come Home(dijkstra算法)
题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ #incl ...
- POJ 2387 Til the Cows Come Home(dijkstra裸题)
题目链接:http://poj.org/problem?id=2387 题目大意:给你t条边(无向图),n个顶点,让你求点1到点n的最短距离. 解题思路:裸的dijsktra,注意判重边. 代码: # ...
- POJ 2387 链式前向星下的SPFA
(POJ)[http://poj.org/problem?id=2387] Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
随机推荐
- Vijos 1243 生产产品 (单调队列优化的动态规划)
题意:中文题.不说了. 注意一些地方,机器的执行过程是没有顺序的,而且每个机器可以用多次.第一次执行的机器不消耗转移时间K. 用dp[i][j]表示第i个机器完成第j个步骤的最短时间,sum[j][i ...
- C++ offsetof
这是一个宏,用于计算类中某个成员的地址相对于类实例的偏移量 在C++11中,要求这个类standard_layout 基本用法是这样子的: #include <stdio.h> /* pr ...
- Tomcat容器运行struts2+spring+mybatis架构的java web应用程序简单分析
1.具体的环境为 MyEclipse 8.5以及自带的tomcat spring3.0.5 struts2.3.15.1 mybatis3.0.5 2.想弄明白的一些问题 tomcat集成spring ...
- poll函数和串口设置
2015.1.24 今天星期六,多云,早晨8:17起床的,今天是来南京起床最迟的一天,因为昨晚睡得有点迟,今天又不用上课,整个人有点放松.收拾好来到教室,教室门没有开,胡明也到了,其他人还在宿舍睡觉, ...
- Connection to DB
Connect to MySQL PHP5 and later can work with a MySQL database using MySQLi extension PDO PDO will w ...
- Android Studio And Gradle
AS特色: 智能感知体验特好,堪比VS 布局预览,手写布局后预览页面即时显示,便于布局调整和优化 编辑速度飞快流畅,毫无eclipse的卡顿 布局或源码中有图标和颜色的预览,十分直观 调试时体验极佳 ...
- The Triangle_DP
ime Limit: 1000MS Memory Limit: 10000K Total Submissions: 45620 Accepted: 27612 Description 73 8 ...
- 脚本语言&& Performance Testing
watin: http://www.cnblogs.com/dahuzizyd/archive/2007/04/13/ruby_on_rails_windows_instatnrails_study_ ...
- 贝努利概率 matlab
参考:http://zhidao.baidu.com/link?url=3XZm35XpFf_kbADwDHEERtgFMKqHftiS5SyTCWcBtlF7B7zeNgoNqIzXxpJsHtBI ...
- Visual Studio 2010 单元测试
转载出处:http://blog.csdn.net/tjvictor/article/details/6175362