https://www.patest.cn/contests/pat-a-practise/1030
找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时。
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<iostream>
#include<queue>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long LL;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int maxn = 5e2 + 10;
int n, m, s, t, map[maxn][maxn], cost[maxn][maxn], x, y, z, c;
int dis[maxn], v[maxn]; void dfs(int x)
{
if (x == t)return;
for (int i = 0; i < n; i++)
{
if (map[x][i])
{
if (dis[i]>dis[x]+map[x][i])
{
dis[i] = dis[x] + map[x][i];
v[i] = v[x] + cost[x][i];
dfs(i);
}
else if (dis[i] == dis[x] + map[x][i] && v[i] > v[x] + cost[x][i])
{
v[i] = v[x] + cost[x][i];
dfs(i);
}
}
}
} bool Dfs(int x)
{
if (x == s){ printf("%d ", s); return true; }
for (int i = 0; i < n; i++)
{
if (map[x][i] && dis[x] == dis[i] + map[x][i] && v[x] == v[i] + cost[x][i])
{
if (Dfs(i)){printf("%d ", x); return true;}
}
}
return false;
} int main()
{
scanf("%d%d%d%d", &n, &m, &s, &t);
while (m--)
{
scanf("%d%d%d%d", &x, &y, &z, &c);
if (!map[x][y] || map[x][y] > z)
{
map[x][y] = map[y][x] = z;
cost[x][y] = cost[y][x] = c;
}
else if (map[x][y] == z) cost[x][y] = cost[y][x] = min(z, cost[x][y]);
}
for (int i = 0; i < n; i++)dis[i] = v[i] = INF;
dis[s] = v[s] = 0;
dfs(s);
Dfs(t);
printf("%d %d\n", dis[t], v[t]);
return 0;
}