ZOJ3946 Highway Project(双边权最短路(?))

讲道理我都不记得我做过啥题了……(其实如果不是因为卡题我也不会写博客的- -)
谢谢帮忙debug(?)的柳总、lhr先生与欣爸爸,还有玥玥
补题感想
点我看题
另外,标题是自己编的

Description

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 ≤ i ≤ N). 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

Solution

比赛时候因为这道题耽误了很多时间(我一直霸着电脑做,间接(?)导致一道水水的模拟没出来
比赛的时候写了半天下线自闭了。赛后第一次补题想记录前驱与这条路的长度,加的时候判断是否访问过。结果肉眼可见的缓慢,T掉了
后来四处寻找代码+提问,终于想(?)到了AC的方法:(求最短路照板敲就ok)每个点只会记录一个最小的花费,并且这个点必定被访问过(不是我想的/我想的差不多但全是bug,柳总和代码真棒的先生告诉我的233)最后相加
后来写出来之后疯狂WA,我哭了。欣爸爸和柳总告诉我n-1条边,最后才AC(其实还有爆int和比较错误的锅,锅太多就不一一说了。再次感谢两位dalao(十分感动

AC代码

我的ma呀,代码都找不着了

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#define inf 0x3f3f3f3f
using namespace std;
long long d[200005];
long long mo[200005];
struct edge
{
    long long from,to,cost,money;
    //bool vis=0;
};
vector<edge>g[200005];
typedef pair<int,int>p;
int main()
{
    int t;
    long long ans;
    while(~scanf("%d",&t))
    {
        for(int k=1;k<=t;++k)
        {
            for(int i=0;i<200005;++i)
            {
                g[i].clear();
            }
            memset(mo,0,sizeof(mo));
            memset(d,inf,sizeof(d));
            //memset(pre,-1,sizeof(pre));
            priority_queue< p,vector<p>,greater<p> >q;
            //int c[200005];
            int n,m;
            scanf("%d %d",&n,&m);
            for(int i=1;i<=m;++i)
            {
                int x,y,d,c;
                scanf("%d %d %d %d",&x,&y,&d,&c);
                edge e1,e2;
                e1.from=x;
                e1.to=y;
                e1.cost=d;
                e1.money=c;
                //e1.vis=0;
                e2.from=y;
                e2.to=x;
                e2.cost=d;
                e2.money=c;
                //e2.vis=0;
                g[x].push_back(e1);
                g[y].push_back(e2);
            }
            d[0]=0;
            mo[0]=0;
            q.push(p(0,0));
            while(!q.empty())
            {
                p f=q.top();
                q.pop();
                int v=f.second;
                if(d[v]<f.first) continue;
                int ll=g[v].size();
                for(int i=0;i<ll;++i)
                {
                    edge e=g[v][i];
                    if(d[e.to]>d[v]+e.cost)
                    {
                        //pre[e.to]=v;
                        d[e.to]=d[v]+e.cost;
                        mo[e.to]=e.money;
                        q.push(p(d[e.to],e.to));
                    }
                    else if(d[e.to]==d[v]+e.cost)
                    {
                        if(mo[e.to]>e.money)
                        {
                            //pre[e.to]=v;
                            mo[e.to]=e.money;
                            //d[e.to]=d[v]+e.cost;
                            // q.push(p(d[e.to],e.to));
                        }
                    }
                    //printf("%d %d %d %d\n",v,mo[v],e.to,mo[e.to]);
                }
            }
            
            /*for(int i=1;i<=m;++i)
             {
             if(b[]==1)
             {
             ans=ans+c[i];
             }
             }*/
            ans=0;
            for(int i=1;i<n;++i)
            {
                if(mo[i]!=inf)
                {
                    ans=ans+mo[i];
                }
            }
            long long ans1=0;
            for(int i=1;i<n;++i)
            {
                if(d[i]!=inf)
                {
                    ans1=ans1+d[i];
                }
            }
            printf("%lld %lld\n",ans1,ans);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值