zoukankan      html  css  js  c++  java
  • patest_1003_Emergency (25)_(dijkstra+dfs)

    1003. Emergency (25)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

    Input

    Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

    Output

    For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
    All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

    Sample Input
    5 6 0 2
    1 2 1 5 3
    0 1 1
    0 2 2
    0 3 1
    1 2 1
    2 4 1
    3 4 1
    
    Sample Output
    2 4

    题意:每个点有点权,每条边有边权,问一个点到另一个点的最短路径(路径上的边权和)有多少条,所有最短路径中路径点权和最大为
    多少。

    思路:dijkstra求最短路,再dfs找条数和最大点权和。

    浙大的陈越老师出的题,会包含很多情况,锻炼自己考虑完善的能力。

    总结:关于图的问题,若题中没特意说明,两点之间可以有多条权值相同或不同的边(用邻接表比较好)。在这道题中,起点和终点应该可以相同。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<stdlib.h>
    #include<vector>
    using namespace std;
    #define N 505
    #define INF 999999999
    
    
    struct Eage
    {
        int v,val,next;
    }eage[N*N];
    int head[N],cnte;
    
    void addEage(int a,int b,int v)
    {
        eage[cnte].v=b;
        eage[cnte].val=v;
        eage[cnte].next=head[a];
        head[a]=cnte++;
    }
    
    int dist[N],cost[N],n,m,val[N];
    bool vis[N];
    int res,cntr;
    
    void dijkstra(int c1,int c2)
    {
        for(int i=0;i<n;i++)
        {
            cost[i]=val[i];
            dist[i]=INF;
            vis[i]=0;
        }
        for(int i=head[c1];i!=0;i=eage[i].next)
        {
            int u=eage[i].v,v=eage[i].val;
            if(dist[u]>v)
                dist[u]=v;
        }
        vis[c1]=1;
        dist[c1]=0;
        for(int i=1;i<n;i++)
        {
            int minn=INF,u;
            for(int j=0;j<n;j++)
                if(minn>dist[j]&&vis[j]==0)
                {
                    minn=dist[j];
                    u=j;
                }
            vis[u]=1;
            for(int j=head[u];j!=0;j=eage[j].next)
            {
                int v=eage[j].v,va=eage[j].val;
                if(dist[v]>dist[u]+va)
    
                    dist[v]=dist[u]+va;
            }
        }
    }
    
    bool vis1[N];
    void dfs(int c1,int c2,int len,int sum)
    {
        if(len>dist[c2])
            return;
        if(c1==c2)
        {
            cntr++;
            res=max(res,sum);
            return;
        }
        for(int i=head[c1];i!=0;i=eage[i].next)
        {
            int v=eage[i].v,va=eage[i].val;
            if(vis1[v]==0)
            {
                vis1[v]=1;
                dfs(v,c2,len+va,sum+val[v]);
                vis1[v]=0;
            }
        }
    }
    
    int main()
    {
        int c1,c2;
        scanf("%d%d%d%d",&n,&m,&c1,&c2);
        for(int i=0;i<n;i++)
            scanf("%d",&val[i]);
        cnte=1;
        for(int i=0;i<m;i++)
        {
            int a,b,v;
            scanf("%d%d%d",&a,&b,&v);
            addEage(a,b,v);
            addEage(b,a,v);
        }
        if(c1==c2)
        {
            printf("1 %d
    ",val[c1]);
            return 0;
        }
        cntr=0;
        dijkstra(c1,c2);
        res=0;
        vis1[c1]=1;
        dfs(c1,c2,0,val[c1]);
        printf("%d %d
    ",cntr,res);
        return 0;
    }
    
    /*
    4 3 3 2
    1 2 0 4
    0 1 1
    1 2 3
    2 3 6
    
    4 4 0 3
    1 2 5 7
    0 1 1
    0 2 1
    1 3 1
    2 3 1
    
    2 3 0 1
    2 3
    0 1 3
    0 1 2
    1 0 2
    
    */
    
    
  • 相关阅读:
    Linux 之 编译器 gcc/g++参数详解
    linux下history命令显示历史指令记录的使用方法
    Linux 命令之 Navicat 连接 Linux 下的Mysql数据库
    Linux命令
    CentOS 下安装
    CMD命令之 :修改windows的CMD窗口输出编码格式为UTF-8
    CTO、技术总监、首席架构师的区别
    PHP ServerPush (推送) 技术的探讨
    一个公司的管理层级结构
    Table of Contents
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/6621945.html
Copyright © 2011-2022 走看看