ZOJ 3946 Highway Project

迪杰斯特拉最小堆

#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;

const long long INF=9999999999999;
const int maxn=2e5+10;
struct X{
    int id;
    long long time,cost;
    bool operator < (const X &a) const
    {
        if(time==a.time) return cost>a.cost;
        return time>a.time;
    }
    X(int f,long long g,long long h)
    {
        id=f,time=g,cost=h;
    }
};

struct Edge
{
    int from,to;
    long long time,cost;
    int next;
}s[maxn];
int head[maxn];
bool flag[maxn];
long long ans1,ans2;
long long dis[maxn];

int main()
{
    int T,n,m; scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);

        memset(head,-1,sizeof head);
        memset(flag,0,sizeof flag);
        ans1=ans2=0;
        for(int i=0;i<=n;i++) dis[i]=INF;

        int cnt=0;
        for(int i=1;i<=m;i++)
        {
            int u,v;
            long long time,cost;
            scanf("%d%d%lld%lld",&u,&v,&time,&cost);

            s[cnt].from=u,s[cnt].to=v,s[cnt].time=time,s[cnt].cost=cost;
            s[cnt].next=head[u],head[u]=cnt++;

            s[cnt].from=v,s[cnt].to=u,s[cnt].time=time,s[cnt].cost=cost;
            s[cnt].next=head[v],head[v]=cnt++;
        }

        priority_queue<X>q;
        q.push(X(0,0,0));  dis[0]=0;

        while(!q.empty())
        {
            X h=q.top(); q.pop();
            if(flag[h.id]==1) continue;

            flag[h.id]=1; ans1=ans1+h.time; ans2=ans2+h.cost;

            for(int i=head[h.id];i!=-1;i=s[i].next)
            {
                if(flag[s[i].to]==0&&dis[s[i].to]>=dis[h.id]+s[i].time)
                {
                    dis[s[i].to]=dis[h.id]+s[i].time;
                    q.push(X(s[i].to,dis[h.id]+s[i].time,s[i].cost));
                }
            }
        }
        printf("%lld %lld\n",ans1,ans2);
    }
    return 0;
}
时间: 2024-07-29 08:23:01

ZOJ 3946 Highway Project的相关文章

ZOJ 3946 Highway Project 贪心+最短路

题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存在一系列边(ui,v)使得dis[v]最小(dis[v]表示0到v的距离).这些边能且只能选一条,那么我们自然应该选cost最小的那个边了. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #inc

zoj 3946 Highway Project spfa

题意:一个帝国有 n 个城市,可以在城市两两之间建立 m 条高速公路,建立 x-y 之间的高速路需要时间 d,花费为 c, 最后建立完边(<=m)后使得首都 0 号城市到各个城市(1~n-1)的总时间最少,在多个时间满足条件下再选花费最少的. 思路:直接spfa,当有多个点使得时间最少时,选花费最小的边. #include <iostream> #include <algorithm> #include <string.h> #include <stdio.

ZOJ-3946 Highway Project (最短路)

题目大意:一张带权无向图,权有两个参数(d,c),分别表示走过这条边的时间和建造这条边的代价.要求选出一些边,使得0节点到其他点的距离之和最短,并在最短的基础上求最小代价. 题目分析:这是16年浙江省赛的一道题.先求出0到所有点的最短路,然后找出所有可能在最短路径上的边,最后在每一个节点的入边之中都选一条具有最小代价的边.多么简单的一道题!!! 代码如下: # include<iostream> # include<cstdio> # include<cstring>

zoj3946--Highway Project

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 highw

2016.4.23 浙江省赛题解

Apples and Ideas Time Limit: 2 Seconds      Memory Limit: 65536 KB "If you have an apple and I have an apple and we exchange these apples then you and I will still each have one apple. But if you have an idea and I have an idea and we exchange these

2016.4.23浙江省赛

A      Apples and Ideas Time Limit: 2 Seconds      Memory Limit: 65536 KB "If you have an apple and I have an apple and we exchange these apples then you and I will still each have one apple. But if you have an idea and I have an idea and we exchange

Team Foundation Server 2013 with Update 3 Install LOG

[Info   @10:14:58.155] ====================================================================[Info   @10:14:58.163] Team Foundation Server Administration Log[Info   @10:14:58.175] Version  : 12.0.30723.0[Info   @10:14:58.175] DateTime : 10/03/2014 18:1

ZOJ 3195 Design the city

倍增法在线LCA..... ZOJ Problem Set - 3195 Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now,

[ACM] ZOJ 3725 Painting Storages (DP计数+组合)

Painting Storages Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob asks you to paint all storages with two colors: red and blue. Each storage will be painted with e