ZJ的偶像包袱

用dfs会错,不明白为什么。

#include<iostream>
#include<vector>
#include<queue>
#include<string.h>
#include<cmath>
using namespace std;
#define ll long long
const unsigned ll INF= 0x3f3f3f3f3f3f3f3f;
const int maxn = 1e5 + 7;
ll dis[maxn];
ll vis[maxn];
struct edge {
    ll u,w;
    bool operator < (edge a) const
    {
        return w>a.w;
    }
}C[maxn];
priority_queue<edge>q;
vector<ll>v[maxn];
vector<ll>w[maxn];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    memset(dis,INF,sizeof(dis));
    int n, m, s, t;
    cin >> n >> m >> s >> t;
    for (int i = 0; i < m; i++) {
        ll a, b, c;
        cin >> a >> b >> c;
        v[a].push_back(b);
        w[a].push_back(c);
        C[a].w = c;
    }
    C[s].u = s;
    C[s].w = 1;
    q.push(C[s]);
    dis[s] = 1;
    while (!q.empty()) {
        edge d = q.top();
        q.pop();
        ll u = d.u;
        vis[u] = 1;
        for (int i = 0; i < v[u].size(); i++)
        {
            if (vis[v[u][i]])
                continue;
            else if (dis[u] * w[u][i]<dis[v[u][i]])
            {
                dis[v[u][i]] = dis[u] * w[u][i];
                d.u = v[u][i];
                d.w = dis[v[u][i]];
                q.push(d);
            }
        }
    }
    if (dis[t] == INF)
        cout << "impossible" << endl;
    else cout << dis[t] << endl;
    return 0;
}

AC代码,dijkstra不断优先队列,不断更新踩实数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值