【codeforces 95C】Volleyball

【题目链接】:http://codeforces.com/problemset/problem/95/C

【题意】

给你n个点,m条边;
每个点有一辆出租车;
可以到达离这个点距离不超过u的点,且在这个距离范围里面,路费都是v;
问你从起点到终点的最小花费;

【题解】

重新建图;
每个点;
连一条边到这个点的出租车能够到达的点(从每个点求最短路);
然后边权都是v;
然后在从这个新建的图上从起点开始跑最短路;

【Number Of WA

0

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1100;
const LL oo = 1e15;

int n,m,s,t,u,v,w;
LL dis[N];
vector <pii> G[2][N];
queue <int> dl;
bool inq[N];

void spfa(int s,int p){
    dl.push(s);
    inq[s] = true;
    rep1(i,1,n) dis[i] = oo;
    dis[s] = 0;
    while (!dl.empty()){
        int x = dl.front();
        inq[x] = false;
        dl.pop();
        int len = G[p][x].size();
        rep1(i,0,len-1){
            pii temp = G[p][x][i];
            int y = temp.fi;
            LL w = temp.se;
            if (dis[y]>dis[x]+w){
                dis[y] = dis[x]+w;
                if (!inq[y]){
                    inq[y] = true;
                    dl.push(y);
                }
            }
        }
    }
}

int main(){
    //Open();
    Close();//scanf,puts,printf not use
    //init??????
    cin >> n >> m;
    cin >> s >> t;
    rep1(i,1,m){
        cin >> u >> v >> w;
        G[0][u].pb(mp(v,w));
        G[0][v].pb(mp(u,w));
    }
    rep1(i,1,n){
        cin >> u >> v;
        spfa(i,0);
        rep1(j,1,n)
            if (j!=i && dis[j]<=u){
                G[1][i].pb(mp(j,v));
            }
    }
    spfa(s,1);
    if (dis[t]>=oo)
        cout <<-1<<endl;
    else
        cout <<dis[t]<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626283.html