题目
输入样例:
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
输出样例:
3 40
思路
狄杰斯特拉几乎算是模版题。练了一下链式前向星。
代码
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 505;
struct Edge{
int to, next, l, w;
}edge[maxn*maxn];
int tot = -1;
int head[maxn*maxn];
int n, m, s, d;
void add(int u, int v, int l, int w) {
++tot;
edge[tot].to = v;
edge[tot].l = l;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot;
}
struct node{
int to, l, w;
node(int to, int l, int w) : to(to), l(l), w(w) {}
};
bool operator<(const node& a, const node& b) {
if(a.l != b.l) {
return a.l > b.l;
}
return a.w > b.w;
}
int dis[maxn];
int con[maxn];
priority_queue<node> que;
void dij() {
memset(dis, 0x3f, sizeof(dis));
memset(con, 0x3f, sizeof(con));
que.push(node(s, 0, 0));
dis[s] = con[s] = 0;
while(que.size()) {
auto tmp = que.top(); que.pop();
for(int i = head[tmp.to]; i != -1; i = edge[i].next) {
int tl = edge[i].l + tmp.l;
int tw = edge[i].w + tmp.w;
if(dis[edge[i].to] > tl) {
dis[edge[i].to] = tl;
con[edge[i].to] = tw;
que.push(node(edge[i].to, tl, tw));
}
else if(dis[edge[i].to] == tl && con[edge[i].to]>tw) {
con[edge[i].to] = tw;
que.push(node(edge[i].to, tl, tw));
}
}
}
}
int main(void)
{
/* freopen("in.txt", "r", stdin); */
scanf("%d%d%d%d", &n, &m, &s, &d);
fill(head, head+n, -1);
int u, v, w, l;
while(m--) {
scanf("%d%d%d%d", &u, &v, &l, &w);
add(u, v, l, w);
add(v, u, l, w);
}
dij();
printf("%d %d\n", dis[d], con[d]);
return 0;
}