本文转载自:http://www.javaxxz.com/thread-359442-1-1.html

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;}

复制代码

ZOJ3946:Highway Project(最短路变形)的更多相关文章

  1. zoj 3946 Highway Project(最短路 + 优先队列)

    Highway Project Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward, the emperor of the Marjar ...

  2. ZOJ - 3946-Highway Project(最短路变形+优先队列优化)

    Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can ...

  3. ZOJ-3946 Highway Project (最短路)

    题目大意:一张带权无向图,权有两个参数(d,c),分别表示走过这条边的时间和建造这条边的代价.要求选出一些边,使得0节点到其他点的距离之和最短,并在最短的基础上求最小代价. 题目分析:这是16年浙江省 ...

  4. ZOJ 3946 Highway Project (最短路)

    题意:单源最短路,给你一些路,给你这些路的长度,给你修这些路的话费,求最短路和最小花费. 析:本质就是一个最短路,不过要维护两个值罢了,在维护花费时要维护的是该路要花多少,而不是总的路线花费. 代码如 ...

  5. POJ-1797Heavy Transportation,最短路变形,用dijkstra稍加修改就可以了;

    Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K          Description Background  Hugo ...

  6. POJ-2253.Frogger.(求每条路径中最大值的最小值,最短路变形)

    做到了这个题,感觉网上的博客是真的水,只有kuangbin大神一句话就点醒了我,所以我写这篇博客是为了让最短路的入门者尽快脱坑...... 本题思路:本题是最短路的变形,要求出最短路中的最大跳跃距离, ...

  7. POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...

  8. POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...

  9. (spfa) Highway Project (zoj 3946 )

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718   Highway Project Time Limit: 2 Seco ...

随机推荐

  1. [译]前端JS面试题汇总 Part 1(事件委托/this关键字/原型链/AMD与CommonJS/自执行函数)

    原文:https://github.com/yangshun/front-end-interview-handbook/blob/master/questions/javascript-questio ...

  2. Java Integer类型比较

    今天做了一道题目题目如下: Integer a=10; Integer b=10; System.out.print(a==b); Integer c=200; Integer d=200; Syst ...

  3. Spring中的注解@Service @Component @Controller @Repository区别

    @Service用于标注业务层组件, @Controller用于标注控制层组件(如struts中的action), @Repository用于标注数据访问组件,即DAO组件, @Component泛指 ...

  4. springmvc 对 jsonp 的支持

    在与前端开发人员合作过程中,经常遇到跨域名访问的问题,通常我们是通过jsonp调用方式来解决.jsop百科:http://baike.baidu.com/link?url=JKlwoETqx2uuKe ...

  5. yii2 查询构建器

    Query Builder $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name']) ->from('zs_dynast ...

  6. Mysql引擎中MyISAM和InnoDB的区别有哪些?

    简单的概括一下 InnoDB:支持事务处理等不加锁读取支持外键支持行锁不支持FULLTEXT类型的索引不保存表的具体行数,扫描表来计算有多少行DELETE 表时,是一行一行的删除InnoDB 把数据和 ...

  7. PHP封装的一个单例模式Mysql操作类

    掌握满足单例模式的必要条件----三私一公. ①私有的构造方法-为了防止在类外使用new关键字实例化对象. ②私有的成员属性-为了防止在类外引入这个存放对象的属性. ③私有的克隆方法-为了防止在类外通 ...

  8. acdrem1083 人民城管爱人民 DP

    思路:d(i, 0)表示从节点i到达大运村的最短路径,d(i, 1)表示从节点i到达大运村的次短路径. 1.最短路:当做DAG处理即可. 2.次短路:假设当前在u点处,下一个节点是v.v到终点的最短路 ...

  9. EntityFramework Core 2.0全局过滤(HasQueryFilter)

    前言 EntityFramework Core每一次版本的迭代和更新都会带给我们惊喜,每次都会尽量满足大部分使用者的需求.在EF Core 2.0版本中出现了全局过滤新特性即HasQueryFilte ...

  10. BIOS简介

    BIOS简介: BIOS是英文"Basic Input Output System"的缩略词,直译过来后中文名称就是"基本输入输出系统".其实,它是一组固化到计 ...