ZOJ 3946 Highway Project(spfa最短路+记忆化搜索)

Highway Project

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can reach other cities from the capital as fast as possible. Thus, he proposed the highway project.

The Marjar Empire has N cities (including the capital), indexed from 0 to N - 1 (the capital is 0) and there are M highways can be built. Building the i-th highway costs Ci dollars. It takes Di minutes to travel between city Xi and Yi on the i-th highway.

Edward wants to find a construction plan with minimal total time needed to reach other cities from the capital, i.e. the sum of minimal time needed to travel from the capital to city i (1 ≤ iN). Among all feasible plans, Edward wants to select the plan with minimal cost. Please help him to finish this task.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first contains two integers N, M (1 ≤ N, M ≤ 105).

Then followed by M lines, each line contains four integers Xi, Yi, Di, Ci (0 ≤ Xi, Yi < N, 0 < Di, Ci < 105).

Output

For each test case, output two integers indicating the minimal total time and the minimal cost for the highway project when the total time is minimized.

Sample Input
2
4 5
0 3 1 1
0 1 1 1
0 2 10 10
2 1 1 1
2 3 1 2
4 5
0 3 1 1
0 1 1 1
0 2 10 10
2 1 2 1
2 3 1 2
Sample Output
4 3
4 4

题意:给出一张图,边无向有两个,两个权分别为路的消耗的时间和花费的经前,点编号0-n-1,现在要你求从0建立到其他的路径,
使各点花费总时间最少优先的情况下花费的金钱也最少。
分析:首先是花费的时间问题,这个很好解决,直接spfa跑一边最短路即可,关键在于花费的金钱问题,因为存在同一消耗的时间
下存在其他路径使建路的金钱更少的情况,所以这里如果从0点正向到各个点考虑会很难处理,现在我们已经知道了所有点的消耗时间,
不放倒过来想,让除0的所有点到0取,那么知道满足时间优先条件的边都可以考虑到,此时记忆化搜索时就只需要取最小的金钱花费,
然后标记当前已经访问过的点,O(n)再遍历一遍图即可
另外要注意这里的数据可能爆int

#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
//#pragma comment(linker, "/STACK:1024000000,1024000000");

using namespace std;

#define INF 0x3f3f3f3f
#define maxn 400005

int n,m;
int fir[maxn],nex[maxn],u[maxn],v[maxn],w[maxn],t[maxn];
long long dis[maxn];
int vis[maxn];
int e_max;

void init()
{
    memset(fir,-1,sizeof fir);
    e_max=0;
}

void add_edge(int s,int r,int ti,int mi)
{
    int e=e_max++;
    u[e]=s;
    v[e]=r;
    t[e]=ti;
    w[e]=mi;
    nex[e]=fir[s];
    fir[s]=e;
}

long long tim,money;
int que[maxn];

void dfs(int k)
{
    if(k==0||vis[k]) return ;
    vis[k]=1;
    long long temp=1e15;
    int pos;
    for(int i=fir[k]; ~i; i=nex[i])
    {
        int p=v[i];
        if(dis[p]+t[i]==dis[k])
        {
            if(w[i]<temp) temp=w[i],pos=v[i];
        }
    }
    money+=temp;
    dfs(pos);
}

void spfa()
{
    int f=0,r=-1;
    memset(vis,0,sizeof vis);
    memset(dis,0x3f,sizeof dis);
    que[++r]=0;
    dis[0]=0;
    while(f<=r)
    {
        int k=que[f++];
        vis[k]=0;
        for(int i=fir[k]; ~i; i=nex[i])
        {
            int p=v[i];
            if(dis[k]+t[i]<dis[p])
            {
                dis[p]=dis[k]+t[i];
                if(!vis[p]) que[++r]=p,vis[p]=1;
            }
        }
    }
    memset(vis,0,sizeof vis);
    tim=0,money=0;
    for(int i=1; i<=n-1; i++)
    {
        tim+=dis[i];
        dfs(i);
    }
    printf("%lld %lld\n",tim,money);
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(int i=0; i<m; i++)
        {
            int a,b,c,d;
            scanf("%d%d%d%d",&a,&b,&c,&d);
            add_edge(a,b,c,d);
            add_edge(b,a,c,d);
        }
        spfa();
    }
    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值