Description

Bobo 居住在大城市 ICPCCamp。

ICPCCamp 有 n 个地铁站,用 1,2,…,n 编号。 m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci 号线,位于站 ai,bi 之间,往返均需要花费
ti分钟(即从 ai 到 bi 需要
ti 分钟,从 bi 到 ai 也需要
ti 分钟)。
众所周知,换乘线路很麻烦。如果乘坐第 i 段地铁来到地铁站 s,又乘坐第 j 段地铁离开地铁站 s,那么需要额外花费 |ci-cj |
分钟。注意,换乘只能在地铁站内进行。
Bobo 想知道从地铁站 1 到地铁站 n 所需要花费的最小时间。

Input

输入包含不超过 20 组数据。
每组数据的第一行包含两个整数 n,m (2≤n≤105,1≤m≤105).
接下来 m 行的第 i 行包含四个整数 ai,bi,ci,ti (1≤ai,bi,ci≤n,1≤ti≤109).
保证存在从地铁站 1 到 n 的地铁线路(不一定直达)。

Output

对于每组数据,输出一个整数表示要求的值。

Sample Input

3 3
1 2 1 1
2 3 2 1
1 3 1 1
3 3
1 2 1 1
2 3 2 1
1 3 1 10
3 2
1 2 1 1
2 3 1 1

Sample Output

1
3
2
Dijkstra 在求最短路的时候可以 以边来求最短路,这是以前没有遇到过的。有时候图论中对点操作不正确的时候可以对边做操作
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <string.h>
#include <stdio.h>
#include <queue> using namespace std;
const int maxn=1e5;
typedef long long int LL;
const LL INF=0x3f3f3f3f3f3f3f3f;
struct node
{
int next;
int value;
LL weight;
LL c;
}edge[maxn*2+5];
int head[maxn*2+5];
int tot;
int vis[maxn+5];
LL d[maxn*2+5];
int n,m;
void add(int x,int y,int w,int c)
{
edge[tot].value=y;
edge[tot].weight=w;
edge[tot].c=c;
edge[tot].next=head[x];
head[x]=tot++;
}
struct Node
{
int id;
LL dis;
Node(){};
Node(int id,LL dis)
{
this->id=id;
this->dis=dis;
}
friend bool operator <(Node a,Node b)
{
return a.dis>b.dis;
}
};
LL Dijkstra()
{
priority_queue<Node> q;
for(int i=0;i<tot;i++)
d[i]=INF;
LL ans=INF;
for(int i=head[1];i!=-1;i=edge[i].next)
{
d[i]=edge[i].weight;
q.push(Node(i,d[i]));
}
while(!q.empty())
{
Node term=q.top();
q.pop();
int p=edge[term.id].value; if(p==n)
ans=min(ans,term.dis);
for(int i=head[p];i!=-1;i=edge[i].next)
{
if(d[i]>term.dis+edge[i].weight+abs(edge[i].c-edge[term.id].c))
{
d[i]=term.dis+edge[i].weight+abs(edge[i].c-edge[term.id].c);
q.push(Node(i,d[i]));
} } }
return ans;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int x,y,w,c;
memset(head,-1,sizeof(head));
tot=0;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d%d",&x,&y,&c,&w);
add(x,y,w,c);
add(y,x,w,c);
} printf("%lld\n", Dijkstra());
}
return 0;
}


CSU 1808 地铁 (Dijkstra)的更多相关文章

  1. CSU 1808: 地铁 最短路

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1808 1808: 地铁 Time Limit: 5 SecMemory Limit: ...

  2. CSU 1808 - 地铁 - [最短路变形]

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 Time limit: 5000 ms Memory limit: 13107 ...

  3. CSU 1808 地铁(最短路变形)

    http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 题意: Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站, ...

  4. 【最短路】【STL】CSU 1808 地铁 (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1808 题目大意: N个点M条无向边(N,M<=105),每条边属于某一条地铁Ci ...

  5. CSU 1808 地铁

    题意: ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci 号线,位于站 ai,bi 之间,往返均需要花费 ti 分钟 ...

  6. CSU 1808:地铁(Dijkstra)

    http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 题意:…… 思路:和之前的天梯赛的一题一样,但是简单点. 没办法直接用点去算.把边看成点 ...

  7. CSU1808 地铁 —— dijkstra变形

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 题解:由于中转线路需要花费一定的时间,所以一般的以顶点为研究对象的dijkst ...

  8. 地铁 Dijkstra(优先队列优化) 湖南省第12届省赛

    传送门:地铁 思路:拆点,最短路:拆点比较复杂,所以对边进行最短路,spfa会tle,所以改用Dijkstra(优先队列优化) 模板 /******************************** ...

  9. CSUOJ 1808 地铁

    Description Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci ...

随机推荐

  1. Intellij IDEA Module 的Language Level的问题

    最近从github上fork了张开涛的Shiro代码,IDE是Intellij IDEA.发现无论是Project还是Module,默认的Language Level都是JDK 1.5,而且每次修改都 ...

  2. HTML <a> 标签的状态和 target 属性

    <a>的四种状态 A:link  连接平常状态 A:hover  鼠标放上去的时候 A:active  鼠标按下的时候 A:visited 连接被访问过后的状态 target属性 _bla ...

  3. C#操作缓存--CacheHelper缓存帮助类

    /// <summary>/// 类说明:Assistant/// 联系方式:361983679  /// 更新网站:<a href=\"http://www.cckan. ...

  4. weblogic11g重置控制密码

    Reset the AdminServer Password in WebLogic 11g and 12c If you forget the AdminServer password for yo ...

  5. 百度地图sdk问题 error inflating class com.baidu.mapapi.map.mapview

    最近在封装开发中,有机会遇到问题还是记录下吧 但是其实都是一个原因  就是 初始化 在MyAplication  onCreate()中加入 SDKInitializer.initialize(get ...

  6. 基于Cocos2d-x学习OpenGL ES 2.0系列——你的第一个立方体(5)

    在上篇文章中,我们介绍了VBO索引的使用,使用VBO索引可以有效地减少顶点个数,优化内存,提高程序效率. 本教程将带领大家一起走进3D--绘制一个立方体.其实画立方体本质上和画三角形没什么区别,所有的 ...

  7. Spring 工作流程简单介绍

    Spring Web MVC 处理Http请求的大致过程: 一旦Http请求到来,DispatcherSevlet将负责将请求分发. DispatcherServlet可以认为是Spring提供的前端 ...

  8. UVa 10905 - Children's Game(求多个正整数排列后,所得的新的数字的极值)

    4thIIUCInter-University Programming Contest, 2005 A Children’s Game Input: standard input Output: st ...

  9. LeetCode——Peeking Iterator

    Description: Given an Iterator class interface with methods: next() and hasNext(), design and implem ...

  10. Cisco IOS和IOS XE 新漏洞检测与修复

    Cisco IOS/IOS XE 新漏洞检测与修复 CVE-2018-0150 Cisco IOS XE 存在默认弱口令 漏洞影响: 默认弱口令可以导致攻击者直远程登录控制Cisco设备.受影响版本, ...