zoukankan      html  css  js  c++  java
  • ZOJ 3946.Highway Project(The 13th Zhejiang Provincial Collegiate Programming Contest.K) SPFA

    ZOJ Problem Set - 3946
    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 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 cityi (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 NM (1 ≤ NM ≤ 105).

    Then followed by M lines, each line contains four integers XiYiDiCi (0 ≤ XiYi < N, 0 < DiCi < 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

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3946

    
    
    题意:求最短时间和最少花费。先满足最短时间,再满足最少花费。

    思路:n,m很大,要用SPFA算法,不能用邻接表,否则会爆内存。

    代码:
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <map>
    using namespace std;
    const int MAXN = 1e5+100, mod = 1e9 + 7, inf = 0x3f3f3f3f;
    typedef long long ll;
    const ll INF = (1ll<<50);
    struct node
    {
        int to,d,c;
    } edge[2*MAXN];
    int head[MAXN],next[2*MAXN];
    int sign[MAXN];
    queue<int>Q;
    ll dist[MAXN],cost[MAXN];
    int n,m;
    void add(int i,int u,int v,int d,int c)
    {
        edge[i].to=v;
        edge[i].d=d;
        edge[i].c=c;
        next[i]=head[u];
        head[u]=i;
    }
    void SPFA(int v)
    {
        int i,u;
        for(i=0; i<n; i++)
        {
            dist[i]=INF;cost[i]=INF;
            sign[i]=0;
        }
        dist[v]=0;
        cost[0]=0;
        Q.push(v);
        sign[v]=1;
        while(!Q.empty())
        {
            u=Q.front();
            Q.pop();
            sign[u]=0;
            i=head[u];
            while(i!=-1)
            {
                if(dist[edge[i].to]>dist[u]+edge[i].d)
                {
                    dist[edge[i].to]=dist[u]+edge[i].d;
                    cost[edge[i].to]=edge[i].c;
                    if(!sign[edge[i].to])
                    {
                        Q.push(edge[i].to);
                        sign[edge[i].to]=1;
                    }
                }
                else if(dist[edge[i].to]==dist[u]+edge[i].d)
                {
                    if(cost[edge[i].to]>edge[i].c)
                        cost[edge[i].to]=edge[i].c;
                }
                i=next[i];
            }
        }
    }
    int main()
    {
        int i,j,T;
        int x,y,d,c;
        cin>>T;
        while(T--)
        {
            cin>>n>>m;
            memset(edge,0,sizeof(edge));
            memset(next,0,sizeof(next));
            for(i=0; i<=n; i++) head[i]=-1;
            j=1;
            for(i=1; i<=m; i++)
            {
                scanf("%d%d%d%d",&x,&y,&d,&c);
                add(j,x,y,d,c);j++;
                add(j,y,x,d,c);j++;
            }
            SPFA(0);
            ll ans1=0,ans2=0;
            for(i=1; i<n; i++)
            {
                if(dist[i]!=INF) ans1+=dist[i];
                if(cost[i]!=INF) ans2+=cost[i];
            }
            cout<<ans1<<" "<<ans2<<endl;
        }
        return 0;
    }
    SPFA
     
    I am a slow walker,but I never walk backwards.
  • 相关阅读:
    wpf 中radioButton binding enum的时候的一个bug
    PLSQL创建Oracle定时任务
    [Transfer]ASP.NETV2.0快速入门、视频在线教程、个人网站、电子商务、宠物店等的使用
    [原创][VB]迷迷糊糊做成的DataList的分页关键字查询,自动编号,URL链接
    [Transfer][VB]DataGrid分页机制,通过编程做一个十分有个性、功能十分完善的分页效果
    [Transfer]声明中“AutoEventWireup”、“Codebehind”、“Inherits”分别表示什么意思?
    [Transfer]二种嵌套的DataGrid数据表格方法[DataRelation,FindControl]
    [Transfer]DataList 自定义分页 (C#)
    [Transfer]Visual Studio2005入门.Net系列视频教程
    JS 屏蔽大全
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/5445385.html
Copyright © 2011-2022 走看看