zoukankan      html  css  js  c++  java
  • PAT1003—— Emergency

    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

    dfs就行。注意是无向图,无向图,无向图!然后实参传错了,检查了一个小时TAT,真想抽自己。。
    #include <iostream>
    #include <cstdio>
    #include <climits>
    #include <cstring>
    #include <algorithm>
    #include <cctype>
    #include <cstdlib>
    #include<cmath>
    #include <string>
    #include <map>
    #include <set>
    #include <queue>
    #include <vector>
    #include <stack>
    #include <cctype>
    using namespace std;
    typedef unsigned long long ull;
    
    
    int s,e,t[502],l[502][502],visit[502],n;
    int tlen,tteam,num;
    
    int dfs(int s,int team,int len)
    {
        if(len>tlen)
            return 0;
        int i;
        if(s==e)
        {
            if(len<tlen)
            {
                num=1;
                tlen=len;
                tteam=team;
            }
            else if(len==tlen)
            {
                ++num;
                tteam=max(tteam,team);
            }
            return 0;
        }
        for(i=0;i<n;++i)
        {
            if(!visit[i]&&l[s][i]!=INT_MAX)
            {
                visit[i]=1;
                dfs(i,team+t[i],len+l[s][i]);
                visit[i]=0;
            }
        }
        return 0;
    }
    
    int main()
    { 
        int x,y,m,i,j,k,ans;
        
        cin>>n>>m>>s>>e;
        for(i=0;i<n;++i)
        {
            visit[i]=0;
            for(j=0;j<n;++j)
            {
                l[i][j]=INT_MAX;
            }
        }
        num=0;
        tlen=INT_MAX;    
        for(i=0;i<n;++i)
        {
            scanf("%d",&t[i]);
        }
        for(i=0;i<m;++i)
        {
            scanf("%d%d%d",&x,&y,&k);
            l[x][y]=l[y][x]=min(k,l[x][y]);
        }
        visit[s]=1;
        dfs(s,t[s],0);
        cout<<num<<" "<<tteam;
        
        return 0;
    }
  • 相关阅读:
    C# 之泛型详解
    C#方法的六种参数,值参数、引用参数、输出参数、参数数组、命名参数、可选参数
    在C#中理解和实现策略模式的绝对入门教程
    负载均衡的原理
    C#多线程编程
    深入研究虚幻4反射系统实现原理(二)
    深入研究虚幻4反射系统实现原理(一)
    虚幻4属性系统(反射)翻译
    UE4中使用数据表(Data Table)
    UE4 自定义物理表面类型(Surface Type)
  • 原文地址:https://www.cnblogs.com/Traveller-Leon/p/5004512.html
Copyright © 2011-2022 走看看