ford 超时  使用优先队列的Dijkstra 算法

//#include <cstdio>
//#include <cstring>
//#include <algorithm>
//#define INF 500000000
//using namespace std;
//int a[20010][20010];
//int main()
//{
// int t, ca = 1;
// scanf("%d",&t);
// while(t--)
// {
// int n,m,s,v;
// scanf("%d%d%d%d",&n,&m,&s,&v);
// for(int i = 0; i <= n; i++)
// for(int j = 0; j <= n; j++)
// {
// if(i == j)
// a[i][j] = 0;
// else
// a[i][j] = INF;
// }
// for(int i = 0; i < m; i++)
// {
// int x,y,w;
// scanf("%d%d%d",&x,&y,&w);
// a[x][y] = a[y][x] = w;
// }
// for(int k = 0; k < n; k++)
// for(int i = 0; i < n; i++)
// for(int j = 0; j < n; j++)
// {
// a[i][j] = min(a[i][j], a[i][k]+a[k][j]);
// }
// if(a[s][v] < INF)
// printf("Case #%d: %d\n",ca++,a[s][v]);
// else
// printf("Case #%d: unreachable\n",ca++);
// }
// return 0;
//}
#include <cstdio>
#include <cstring>
#include <queue> #define N 20005
#define M 100005
#define INF 1<<28 struct node
{
int id, dd;
node(int i, int j)
{
id = i;
dd = j;
}
bool operator < (const node &it) const
{
return dd > it.dd;
}
}; int first[M],v[M],w[M],next[M],dis[N],vis[N]; int Dijkstra(int st, int ed, int n)
{
for(int i = 0; i < n; i++)
{
vis[i] = 0;
dis[i] = INF;
}
dis[st] = 0;
std::priority_queue<node> Q;
Q.push(node(st, 0));
while(!Q.empty())
{
node t = Q.top();
Q.pop();
if(vis[t.id]) continue;
if(t.id == ed)
return t.dd;
vis[t.id] = 1;
for(int e = first[t.id]; e != -1; e = next[e])
{
if(dis[v[e]] > dis[t.id] + w[e])
{
dis[v[e]] = dis[t.id] + w[e];
Q.push(node(v[e], dis[v[e]]));
}
}
}
return INF;
} int main()
{
int ca = 1;
int t;
scanf("%d", &t);
while(t--)
{
int n,m,s,d;
scanf("%d%d%d%d", &n, &m, &s, &d);
memset(first, -1, sizeof(first));
int l = 0;
for(int i = 0; i < m; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
next[l] = first[a];
v[l] = b;
w[l] = c;
first[a] = l++;
next[l]=first[b];
v[l] = a;
w[l] = c;
first[b] = l++;
}
int ans = Dijkstra(s, d, n);
printf("Case #%d: ", ca++);
if(ans == INF)
puts("unreachable");
else
printf("%d\n", ans);
}
return 0;
}

uva 10986的更多相关文章

  1. UVA.10986 Fractions Again (经典暴力)

    UVA.10986 Fractions Again (经典暴力) 题意分析 同样只枚举1个,根据条件算出另外一个. 代码总览 #include <iostream> #include &l ...

  2. uva 10986 - Sending email(最短路Dijkstra)

    题目连接:10986 - Sending email 题目大意:给出n,m,s,t,n表示有n个点,m表示有m条边,然后给出m行数据表示m条边,每条边的数据有连接两点的序号以及该边的权值,问说从点s到 ...

  3. UVa 10986 - Sending email

    题目大意:网络中有n个SMTP服务器,有m条电缆将它们相连,每条电缆传输信息需要一定的时间.现在给出信息的起点和终点,计算所需的最小时间. 有权图上的单源最短路问题(Single-Source Sho ...

  4. UVA 10986 Sending email 最短路问题

    基本的最短路问题 就是数据需要稍微处理一下.(N比较大)dijkstra也要优化.不优化应该会T: #include <map> #include <set> #include ...

  5. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  6. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  7. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  8. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  9. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

随机推荐

  1. 函数 datediff(根据objid 获取同name 同年度最近的4条记录)

    显示 包括选择的这条,在加上 选择年度的此人 最近的 3条.(最多显示4条) . 记录数大于4条 . 全显示 create table temp( objid ,) primary key , nam ...

  2. 第六十四篇、OC_计步器

    计步器的实现方式主要有那么两种 1.通过直接调用系统的健康数据,基于HealthKit框架的,但是貌似是一小时更新一次数据.如果要实时获取步数,这种方式并不是最佳. 2.基于CoreMotion框架, ...

  3. OC2_点语法(属性关键字)

    // // Dog.h // OC2_点语法(属性关键字) // // Created by zhangxueming on 15/6/16. // Copyright (c) 2015年 zhang ...

  4. C#抽象工厂简单实现类

    曾经参与开发过的的项目,一般都是采用MVC模式进行开发,大概框架图如下: web界面层调用BLL业务层,BLL通过抽象工厂DALFactory动态生成继承了IDAL的数据库操作层实例,以进行对数据库的 ...

  5. [设计模式]NetworkManagementService中的观察者模式

    观察者模式 观察者模式有如下角色 (1)被观察者(Subject) (2)观察者(Observer) public class Subject{ private: list<Observer&g ...

  6. 基于FPGA的按键扫描程序

    最近在学习FPGA,就试着写了个按键扫描的程序.虽说有过基于单片机的按键扫描处理经验,对于按键的处理还是有一些概念.但是单片机程序的编写通常都采用C写,也有用汇编,而FPGA却是采用VHDL或者Ver ...

  7. SQL Server 收缩事务日志的方法

    由于SQL2008对文件和日志管理进行了优化,所以以下语句在SQL2005中可以运行但在SQL2008中已经被取消:(SQL2005) Backup Log DataBaseName with no_ ...

  8. 【winform】如何在DateTimePicker中显示时分秒

    1. 首先属性里面的Format属性value设置为Custom(默认为Long) 2. 对应的Custom属性value设置为yyyy-MM-dd HH:mm:ss

  9. C# CRC32

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  10. 搭建pptpd实现vpn

    PPTP(Point to Point Tuneling Protocol,点对点隧道协议)是一种主要用于VPN的数据链路层网络协议. 环境:debian 7.0 在linux下安装pptpd服务实现 ...