# CSU1808 地铁 （最短路，好题）

## 题目链接

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
const int inf = 2e9;
const long long onf = 1e18;
#define me(a, b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI 3.14159265358979323846
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int n, m;
ll dis[maxn << 1];
struct node {
int v, next, val, c;
} maps[maxn << 1];

struct Node {
int i;
ll val;

bool friend operator<(Node a, Node b) {
return a.val > b.val;
}
};

void add_edge(int u, int v, int val, int c) {
maps[tot] = node{v, head[u], val, c};
}

void ins(int u, int v, int val, int c) {
}

ll Dijkstra() {
bool vis[maxn << 1];
for (int i = 0; i <= tot; i++)///初始化这个不是顶点的数目，是边的数目。
dis[i] = onf, vis[i] = 0;
priority_queue<Node> q;
for (int i = head[1]; i != -1; i = maps[i].next) {///首先把跟起点连接的边放到队列里面。
dis[i] = maps[i].val;
q.push(Node{i, dis[i]});
}
ll ans = onf;
while (!q.empty()) {
Node cur = q.top();
q.pop();
if (vis[cur.i])
continue;
vis[cur.i] = 1;
int u = maps[cur.i].v;
if (u == n)
ans = min(ans, dis[cur.i]);
for (int i = head[u]; i != -1; i = maps[i].next) {
int temp = abs(maps[cur.i].c - maps[i].c);
int val = maps[i].val;
if (dis[i] > dis[cur.i] + val + temp) {
dis[i] = dis[cur.i] + val + temp;
q.push(Node{i, dis[i]});
}
}
}
return ans;
}

void init() {
for (int i = 1; i <= n; i++)
tot = 0;
}

void work() {
while (scanf("%d%d", &n, &m) != EOF) {
init();
for (int i = 1; i <= m; i++) {
int a, b, c, t;
scanf("%d%d%d%d", &a, &b, &c, &t);
ins(a, b, t, c);
}
printf("%lld\n", Dijkstra());
}
}

int main() {
#ifndef ONLINE_JUDGE
//freopen("1.in", "r", stdin);
#endif
int t = 1, Case = 1;
//cin >> t;
while (t--) {
//  printf("Case %d: ", Case++);
work();
}
return 0;
}

©️2020 CSDN 皮肤主题: 创作都市 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。