题目大意
给你一个图,每条边有一个代价,让你求0到1在最短路径的前提下的最小代价
解题思路
bfs同时求个最代价
代码
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define N 200
using namespace std;
int n, m, x, y, z, tot, b[N], p[N], v[N], head[N];
queue<int>d;
struct rec
{
int to, l, next;
}a[N*10];
void add(int x, int y, int z)
{
a[++tot].to = y;
a[tot].l = z;
a[tot].next = head[x];
head[x] = tot;
return;
}
void bfs()
{
memset(b, 127/3, sizeof(b));
memset(v, 127/3, sizeof(v));
b[0] = 0;
v[0] = 0;
p[0] = 1;
d.push(0);
while(!d.empty())//bfs
{
int h = d.front();
d.pop();
for (int i = head[h]; i; i = a[i].next)
if (b[h] + 1 < b[a[i].to] || b[h] + 1 == b[a[i].to] && v[h] + a[i].l < v[a[i].to])//没到过或长度一样且代价更小
{
b[a[i].to] = b[h] + 1;
v[a[i].to] = v[h] + a[i].l;
if (!p[a[i].to])
{
d.push(a[i].to);
p[a[i].to] = 1;
}
}
}
return;
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i)
{
scanf("%d%d%d", &x, &y, &z);
add(x, y, z);
}
bfs();
printf("%d", v[1]);
return 0;
}