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.h>
#include <vector>
#include <queue>
using namespace std;
#define LL long long
#define MAX 999999999999
#define maxn 100005
struct node
{
    int to,nex;
    LL v,cost;
} p[4*maxn];
int h[maxn],cnt,n,m;
LL d[maxn],c[maxn];
bool inq[maxn];
void init()
{
    for(int i=0; i<maxn; i++) inq[i]=0;
    for(int i=0; i<maxn; i++) d[i]=MAX;
    for(int i=0; i<maxn; i++) c[i]=MAX;
    for(int i=0; i<maxn; i++) h[i]=0;
    cnt=0;
}
void add(int u,int v,LL w,LL cc)
{
    cnt++;
    p[cnt].v=w;
    p[cnt].cost=cc;
    p[cnt].to=v;
    p[cnt].nex=h[u];
    h[u]=cnt;
}
void spfa()
{
    queue<int>q;
    q.push(0);
    d[0]=0;
    inq[0]=1;
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        inq[now]=0;
        for(int i=h[now]; i>0; i=p[i].nex)
        {
            if(d[p[i].to]>d[now]+p[i].v)
            {
                d[p[i].to]=d[now]+p[i].v;
                c[p[i].to]=p[i].cost;
                if(inq[p[i].to]==0)
                {
                    inq[p[i].to]=1;
                    q.push(p[i].to);
                }
            }
            else if(d[p[i].to]==d[now]+p[i].v)
            {
                if(c[p[i].to]>p[i].cost)
                {
                    c[p[i].to]=p[i].cost;
                    if(inq[p[i].to]==0)
                    {
                        inq[p[i].to]=1;
                        q.push(p[i].to);
                    }
                }
            }
        }
    }
    return;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        init();
        cin>>n>>m;
        for(int i=0; i<m; i++)
        {
            int x,y;
            LL w,z;
            scanf("%d%d%lld%lld",&x,&y,&w,&z);
            add(x,y,w,z);
            add(y,x,w,z);
        }
        spfa();
        LL ans1=0,ans2=0;
        for(int i=1;i<n;i++)
            ans1+=d[i],ans2+=c[i];
        printf("%lld %lld\n",ans1,ans2);
    }
    return 0;
}
时间: 2024-08-24 00:25:11

zoj 3946 Highway Project spfa的相关文章

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

迪杰斯特拉最小堆 #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 ti

ZOJ 3794 Greedy Driver spfa

题意: 给定n个点,m条有向边,邮箱容量. 起点在1,终点在n,开始邮箱满油. 下面m行表示起点终点和这条边的耗油量(就是长度) 再下面给出一个数字m表示有P个加油站,可以免费加满油. 下面一行P个数字表示加油站的点标. 再下面一个整数Q 下面Q行 u v 表示在u点有销售站,可以卖掉邮箱里的任意数量的油,每以单位v元. 问跑到终点能获得最多多少元. 先求个每个点的最大剩余油量 f[i], 再把边反向,求每个点距离终点的最短路 dis[i]. 然后枚举一下每个销售点即可,( f[i] - dis

Zoj 3088 Easter Holidays SPFA+枚举

其实就是枚举最高点和起点,然后以最高点为源点在两张图上分别做spfa.一遍最短路,一遍最长路. 暴露出来的问题:思维不够清晰,代码能力还不够 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include &

ZOJ 2770 差分约束+SPFA

Burn the Linked Camp Time Limit: 2 Seconds      Memory Limit: 65536 KB It is well known that, in the period of The Three Empires, Liu Bei, the emperor of the Shu Empire, was defeated by Lu Xun, a general of the Wu Empire. The defeat was due to Liu Be

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