[CF938D] Buy a Ticket - the shortest, FIG built

In \ (n \) city concert, which \ (n \) cities, people want to go to a concert, different fares in each city, so if these people wanted to go to a concert to listen to other cities cheaper (go back every toll)

Solution

Concert set to \ (0 \) dot

Even edge \ (0 \ to i \) , for a pair up cities, even edge \ (u \ leftrightarrow v \)

Shortest can run

#include <bits/stdc++.h>
using namespace std;
#define reset3f(x) memset(x,0x3f,sizeof x)
#define int long long
namespace sp {
const int N=1e+6+5;
vector<pair<int,int> > g[N];
int n,v0=1,d[N];
void make(int t1,int t2,int t3) {
    g[t1].push_back(make_pair(t2,t3));
}
void reset_graph() {
    for(int i=0;i<=n;i++) g[i].clear();
}
void solve() {
    priority_queue<pair<int,int> > qu;
    reset3f(d);
    d[v0]=0;
    qu.push(make_pair(0,v0));
    while(qu.size()) {
        int p=qu.top().second,r=qu.top().first;
        qu.pop();
        if(r+d[p]) continue;
        for(int i=0;i<g[p].size();i++) {
            int q=g[p][i].first,w=g[p][i].second;
            if(d[q]>d[p]+w) {
                d[q]=d[p]+w;
                qu.push(make_pair(-d[q],q));
            }
        }
    }
}
}

int n,m,t1,t2,t3;

signed main() {
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1;i<=m;i++) {
        cin>>t1>>t2>>t3;
        t3*=2;
        sp::make(t1,t2,t3);
        sp::make(t2,t1,t3);
    }
    for(int i=1;i<=n;i++) {
        cin>>t1;
        sp::make(0,i,t1);
    }
    sp::v0=0;
    sp::solve();
    for(int i=1;i<=n;i++) cout<<sp::d[i]<<" ";
}

Guess you like

Origin www.cnblogs.com/mollnn/p/12587111.html
Recommended