Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 312|回复: 0

[默认分类] ZOJ3946:Highway Project(最短路变形)

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-3-18 23:17:39 | 显示全部楼层 |阅读模式


    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 C[sub]i[/sub] dollars. It takes D[sub]i[/sub] minutes to travel between city X[sub]i[/sub] and Y[sub]i[/sub] 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 Tindicating the number of test cases. For each test case:
    The first contains two integers N, M (1 ≤ N, M ≤ 10[sup]5[/sup]).
    Then followed by&nbsp;M&nbsp;lines, each line contains four integers&nbsp;X[sub]i[/sub],&nbsp;Y[sub]i[/sub],&nbsp;D[sub]i[/sub],&nbsp;C[sub]i[/sub]&nbsp;(0 ≤&nbsp;X[sub]i[/sub],&nbsp;Y[sub]i[/sub]&nbsp;<&nbsp;N, 0 <&nbsp;D[sub]i[/sub],&nbsp;C[sub]i[/sub]&nbsp;< 10[sup]5[/sup]).<h4< dd="">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.<h4< dd="">Sample Input
    1. 2
    2. 4 5
    3. 0 3 1 1
    4. 0 1 1 1
    5. 0 2 10 10
    6. 2 1 1 1
    7. 2 3 1 2
    8. 4 5
    9. 0 3 1 1
    10. 0 1 1 1
    11. 0 2 10 10
    12. 2 1 2 1
    13. 2 3 1 2
    复制代码
    <h4< dd="">Sample Output
    1. 4 34 4
    复制代码


    题意:给一个图,每条边有个距离和花费,要求创建一个子图,满足0点到其余点的距离总和最小,且边的总花费最小。
    思路:条件一,每个点贪心的都选最短路就是最优解,条件二,类似最小生成树的kruskal算法,在条件一的基础上更改下松弛条件即可。
    拓展:CF545E,求距离总和最小,且总的边的长度最小,其实那题也是类似最短路基础上跑最小生成树。
    1. # include <iostream># include <cstdio># include <cstring># include <queue>using namespace std;const int maxn = 2e5+30;long long dis[maxn], cost[maxn], x, y;int vis[maxn], Next[maxn], cnt;struct node{int u, v, w, c, next;}edge[maxn];queue<int>q;int scan(){    int res=0,ch,flag=0;    if((ch=getchar())=="-")        flag=1;    else if(ch>="0"&&ch<="9")        res=ch-"0";    while((ch=getchar())>="0"&&ch<="9")        res=res*10+ch-"0";    return flag?-res:res;}void out(long long x){    if(x/10) out(x/10);    putchar("0"+x%10);}void add(int u, int v, int w, int c){    edge[cnt] = {u, v,w,c,Next[u]};;    Next[u] = cnt++;    edge[cnt] = {v, u,w,c,Next[v]};    Next[v] = cnt++;}void spfa(){    memset(dis, 0x3f, sizeof(dis));    memset(cost, 0x3f, sizeof(cost));    memset(vis, 0, sizeof(vis));    while(!q.empty()) q.pop();    q.push(1);    dis[1] = cost[1] = 0;    while(!q.empty())    {        int u = q.front();        q.pop();        vis[u] = 0;        for(int i=Next[u]; ~i; i=edge[i].next)        {            int v=edge[i].v, w=edge[i].w, c=edge[i].c;            if(dis[v] > dis[u]+w)            {                dis[v] = dis[u]+w;                cost[v] = c;                if(!vis[v])                {                    vis[v] = 1;                    q.push(v);                }            }            else if(dis[v] == dis[u]+w)            {                if(cost[v] > c)                {                    cost[v] = c;                    if(!vis[v])                    {                        vis[v] = 1;                        q.push(v);                    }                }            }        }    }}int main(){    int t, n, m, u, v, w, c;    t=scan();    while(t--)    {        x = y = cnt = 0;        memset(Next, -1, sizeof(Next));        n=scan();m=scan();        for(int i=1; i<=m; ++i)        {            u=scan();v=scan();w=scan();c=scan();            add(u+1,v+1,w,c);        }        spfa();        for(int i=2; i<=n; ++i)        {            x += dis[i];            y += cost[i];        }        out(x);        putchar(" ");        out(y);        puts("");    }    return 0;}
    复制代码


    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-17 07:53 , Processed in 0.397475 second(s), 37 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表