ZOJ 3946 Highway Project (spfa)

来源:互联网 发布:sass for mac中文版 编辑:程序博客网 时间:2024/05/16 11:55

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718

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


0~n-1个城市,然后可以修M条路。走完每条路要Di的时间,修这条路要Ci的花费。
然后让你修一些路使其满足从0到其他每个城市的总的时间花费最短。在这个条件满足的情况下下,找总的最小的花费。


解法: 依题意得,求每个点到0这个点的最短路径,明显spfa。
利用贪心的思想,取每个点的入边的最小花费,相加可得答案。


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int M = 1e5 + 100;const long long INF = 0x3f3f3f3f;typedef long long ll;struct Node{    int next,to;    ll d,c;    Node(){}    Node(int a,int b,ll d,ll c):next(a),to(b),d(d),c(c){}}Edge[M*2];int head[M]; int nedges; ll dis[M];ll in[M],vis[M];void add_edge(int a,int b,ll c,ll d){    Edge[++nedges] = Node(head[a],b,c,d);    head[a] = nedges;    Edge[++nedges] = Node(head[b],a,c,d);    head[b] = nedges;}void spfa(int n){    queue<int>que;    dis[0] = 0;    que.push(0);    vis[0] = true;    while(!que.empty())    {        int now = que.front();        que.pop();        vis[now] = false;        for(int i = head[now];~i;i=Edge[i].next)        {            int v = Edge[i].to;            if(dis[now] + Edge[i].d <= dis[v])            {                if(dis[now] + Edge[i].d == dis[v])                   in[v] = min(in[v],Edge[i].c);                else in[v] = Edge[i].c;                dis[v] = dis[now] + Edge[i].d;                if(!vis[v])                {                    que.push(v);                    vis[v] = true;                }            }        }    }    ll nowl = 0,nowr = 0;    for(int i=1;i<n;i++) nowl += dis[i],nowr += in[i];    printf("%lld %lld\n",nowl,nowr);}int main(){    int T;    scanf("%d",&T);    while(T--)    {        int n,m;        nedges = -1;        memset(head,-1,sizeof(head));        memset(dis,INF,sizeof(dis));        memset(vis,false,sizeof(vis));        memset(in,INF,sizeof(in));        scanf("%d%d",&n,&m);        while(m--)        {            int x,y;            ll d,c;            scanf("%d%d %lld%lld",&x,&y,&d,&c);            add_edge(x,y,d,c);        }        spfa(n);    }    return 0;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 我的世界hqm重置怎么办 不小心打了110怎么办 我的世界皮肤有黑影怎么办 我的世界字体变大了怎么办 生锈的铁钉扎了怎么办 每天晚上窗纱上老有蝙蝠倒挂怎么办 我的世界没有痒怎么办 七日杀被ban了怎么办 吕框箱子上保护摸撕不掉怎么办 我的世界开光影卡怎么办 我的世界买不了怎么办 我的世界延迟高怎么办 我的世界过于昂贵怎么办 白色麻布染上别的颜色怎么办 印度老山檀香开裂了怎么办 专升本没过线怎么办 西安公租房小孩上学怎么办 全民k歌直播没人怎么办 在全民直播没人看怎么办 皮肤又黄又粗怎么办 被强制消费后应怎么办? 当保安不发工资怎么办? 辅警改革流管员怎么办 退伍证上照片毁了怎么办 辅警年龄大了怎么办 交警2小时不出警怎么办 中暑发烧39度了怎么办 中暑头疼怎么办最快最有效 十五个月的宝宝拉肚子怎么办 中暑了头疼想吐怎么办 2周岁中暑了呕吐怎么办 容易中暑的人该怎么办 喷泡3m反光脏了怎么办 新摩托车被交警查到怎么办 写字楼保安夜班巡逻害怕怎么办 全民k歌歌曲删了怎么办 莲藕洞里的黑膜怎么办 鞋子后面的拉链磨脚怎么办 磨砂鞋尖踢掉皮怎么办 塔吊吊装过程中遇到障碍物怎么办 高速路上突然遇到障碍物怎么办