CSU 1808 地铁 (Dijkstra)
Description
Bobo 居住在大城市 ICPCCamp。
ti分钟(即从 ai 到 bi 需要
ti 分钟,从 bi 到 ai 也需要
ti 分钟)。
分钟。注意,换乘只能在地铁站内进行。
Input
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)的更多相关文章
- CSU 1808: 地铁 最短路
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1808 1808: 地铁 Time Limit: 5 SecMemory Limit: ...
- CSU 1808 - 地铁 - [最短路变形]
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 Time limit: 5000 ms Memory limit: 13107 ...
- CSU 1808 地铁(最短路变形)
http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 题意: Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站, ...
- 【最短路】【STL】CSU 1808 地铁 (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1808 题目大意: N个点M条无向边(N,M<=105),每条边属于某一条地铁Ci ...
- CSU 1808 地铁
题意: ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci 号线,位于站 ai,bi 之间,往返均需要花费 ti 分钟 ...
- CSU 1808:地铁(Dijkstra)
http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 题意:…… 思路:和之前的天梯赛的一题一样,但是简单点. 没办法直接用点去算.把边看成点 ...
- CSU1808 地铁 —— dijkstra变形
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 题解:由于中转线路需要花费一定的时间,所以一般的以顶点为研究对象的dijkst ...
- 地铁 Dijkstra(优先队列优化) 湖南省第12届省赛
传送门:地铁 思路:拆点,最短路:拆点比较复杂,所以对边进行最短路,spfa会tle,所以改用Dijkstra(优先队列优化) 模板 /******************************** ...
- CSUOJ 1808 地铁
Description Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci ...
随机推荐
- 【转】WCF入门教程一[什么是WCF]
一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...
- C#多播委托/多播代理
定义:委托是一种在对象里保存方法引用的类型,同时也是一种类型安全的函数指针.理解委托的一种方式可以把委托的作用当作是给方法签名指定名称.委托的定义类似于方法的定义,但没有方法体,定义的委托名前要加上关 ...
- PS流的格式和解析总结
对于PS流,最近因为工作需要,所以MPEG2中的PS流格式和解包过程进行了学习. 首先我们需要知道PS包流格式是怎么样的: (来自http://blog.csdn.net/chen495810242/ ...
- socket client简单传输数据
1.整数转换为用于TCP传输的二进制 _host = "127.0.0.1" _port = 5678 _address = (_host, _port) s=socket.soc ...
- 【Java面试题】1 Java中使用switch-case的用法及注意事项超全总结
今天在用到switch的时候,这种设计到最基本的内容,可能忘记它的一些基本语法,出现了一些错误,所以即兴从各种资料查询总结了下面的内容,希望可以帮助那些正在困扰switch错误和各种细节问题的朋友! ...
- 抽象工厂模式(abstract factory pattern)------创造型模式
创建型模式:抽象工厂模式 引入概念: 1.产品等级结构:当抽象的产品由具体的工厂生产出不同的产品时,这些归属与同一类的抽象产品就构成了产品等级结构: 2.产品族:具体的工厂可以生产出来的不同产品就构成 ...
- .net cs后台刷新aspx页面的四种方式
一:Response.Redirect(Request.Url.ToString()); 二:Response.Write("<script language=javascript&g ...
- Linux上运行Jmeter
上传jmeter到Linux服务器 unzip解压 配置环境变量vi /etc/profile: export PATH=/tmp/apache-jmeter-3.0/bin/:$PATH 刷新环境变 ...
- Gson、FastJson、json-lib对比与实例
一.各个JSON技术的对比(本部分摘抄自http://www.cnblogs.com/kunpengit/p/4001680.html): 1.json-libjson-lib最开始的也是应用最广泛的 ...
- IntersectRect、wcsrchr、CComPtr、GetFileAttributes
IntersectRect 两矩形相交形成的新矩形 The IntersectRect function calculates the intersection of two source re ...