关闭

codeforces95C 最短路套最短路

261人阅读 评论(0) 收藏 举报
分类:

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

题目大意:城市里面有n个交叉路口,m条路,每条无向有长度,每个交叉路口都有一个出租车司机等待,出租车可以行使长度不超过ti的距离,花费为ci。告诉你起始路口,问到达目标路口的最小花费。


思路:求最小花费,可以转化为最短路问题。首先我们可以根据m条路对每个路口出租车司机可以到达的路口和花费建图,然后求新建图的最短路即可。建图时也需要用到最短路,要求出定距离可以到达最多的点。还有就是求最短路时初始值一定要足够大,因为wi是10^9.


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <iomanip>

using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000")
#define maxn 100050
#define MOD 1000000007
#define p 1000000007
#define mem(a , b) memset(a , b , sizeof(a))
#define LL long long
#define ULL unsigned long long
#define FOR(i , n) for(int i = 1 ;  i<= n ; i ++)
typedef pair<int , int> pii;
const long long INF = 0x3fffffff;
int n , m , st , ed;
int vis[1005];
LL cost[1005];
LL ans;

struct node
{
    int v ;
    LL w;
    int flag;
    node(){};
    node(int _v , int _w ):v(_v),w(_w){};
    bool friend operator<(node n1 , node n2)
    {
        return n1.w >= n2.w;
    }
}cur , tmp;
vector<node>v[1005];
vector<node>e[1005];

void dfs(int rt , int s ,LL cost , LL now , LL dis)
{
    //sort(v[s].begin() , v[s].end());
    for(int i = 0 ; i < v[s].size() ; i ++)
    {
        node cur2 = v[s][i];
        if(vis[cur2.v] > 5) continue;
        if(cur2.w + now <= dis)
        {
           // cout << cur2.v << endl;
            e[rt].push_back(node(cur2.v , cost));
            vis[cur2.v] ++;
            dfs(rt , cur2.v , cost , cur2.w + now , dis);
        }
        //else break;
    }
}

void spfa2(int root)
{
    for(int i = 0 ; i < 1005  ; i ++) cost[i] = INF*1000000;
    cost[root] = 0;
    queue<int>Q;
    while(!Q.empty()) Q.pop();
    Q.push(root);
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        for(int i = 0 ; i < v[u].size() ; i ++)
        {
            node cur2 = v[u][i];
            if(cost[cur2.v] > cost[u] + cur2.w)
            {
                cost[cur2.v] = cost[u] + cur2.w;
                Q.push(cur2.v);
            }
        }
    }

}

void spfa(int root)
{
    for(int i = 0 ; i < 1005  ; i ++) cost[i] = INF*1000000;
    cost[root] = 0;
    queue<int>Q;
    while(!Q.empty()) Q.pop();
    Q.push(root);
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        for(int i = 0 ; i < e[u].size() ; i ++)
        {
            node cur2 = e[u][i];
            if(cost[cur2.v] > cost[u] + cur2.w)
            {
                cost[cur2.v] = cost[u] + cur2.w;
                Q.push(cur2.v);
            }
        }
    }

}

void BFS()
{
    priority_queue<node>q;
    while(!q.empty()) q.pop();
    mem(vis , 0);
    for(int i = 0 ; i < e[st].size() ; i ++) q.push(e[st][i]);
    vis[st] = 1;
    while(!q.empty())
    {

        cur = q.top();
        q.pop();
        if(ans >0 && cur.w > ans) break;
        if(cur.v  == ed)
        {
            if(ans != -1)ans = min(cur.w , ans);
            else ans = cur.w;
            //break;
        }
        for(int i = 0 ; i < e[cur.v].size() ; i ++)
        {
            tmp = e[cur.v][i];
            if(!vis[tmp.v])
            {
                q.push(node(tmp.v , cur.w + tmp.w));
                vis[tmp.v] ++;
            }
        }
    }
}


int main()
{
    while(scanf("%d %d" , &n , &m) != EOF)
    {
        for(int i = 0 ; i < 1005 ;i ++)
        {
            v[i].clear();
            e[i].clear();
        }
        scanf("%d %d" , &st , &ed);
        int a , b ,c;
        for(int i = 0 ; i < m ; i ++)
        {
            scanf("%d %d %d" , &a , &b , &c);
            v[a].push_back(node(b ,c));
            v[b].push_back(node(a ,c));
        }
        for(int i = 1 ; i <= n ; i ++)
        {
            scanf("%d %d" , &a , &b);
          /*  mem(vis ,0);
            vis[i] = 1;
            e[i].push_back(node(i , 0));
            dfs(i , i, b , 0 , a);*/
            e[i].push_back(node(i , 0));
            spfa2(i);
            for(int j = 1 ; j <= n ; j ++)
            {
                if(cost[j] <= a)
                {
                    e[i].push_back(node(j , b));
                }
            }
        }
        if(st == ed)
        {
            printf("0\n");
            continue;
        }
        ans = -1;
        //BFS();
        spfa(st);
        if(cost[ed] != INF*1000000) printf("%lld\n" , cost[ed]);
        else printf("-1\n");

    }
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:48577次
    • 积分:2693
    • 等级:
    • 排名:第13411名
    • 原创:229篇
    • 转载:15篇
    • 译文:0篇
    • 评论:3条
    最新评论