http://pat.zju.edu.cn/contests/pat-practise/1003

5301 단어 PAT
1003. Emergency (25)
시간 제한
400 ms
메모리 제한
32000 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

S 집합 표 시 를 설정 하지 마 세 요.왜 요?그렇지 않 으 면 한 번 만 방문 할 수 있 는데,어떻게 계산 합 니까?
ans 초기 값 은 1 이 어야 합 니 다.그 렇 죠?일반적인 상황 은 모두 옳다.바로 출발점 과 종점 이 같 을 때 틀린 것 이다.자신 이 자신 에 게 가 는 것 은 하나의 길이 고 0 개의 길이 아니다.
점심 에 잠 을 이 루 지 못 해 오후 에 아버 지 를 속 였 다.니 마!또 자신 이 없어!
[cpp]  view plain copy
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
using namespace std;  
#define MAX 0x6FFFFFFF  
int nums[501];  
int map[501][501];  
int dis[501];  
int ps[501];  
struct Node{  
    int dis;  
    int people;  
    int th;  
    bool operatorconst Node& node)const{  
        if(dis!=node.dis)  
            return dis
        return people>node.people;  
    }  
};  
priority_queue q;  
int main(){  
  
    //freopen("in.txt", "r", stdin);  
  
    int n, m;  
    int s, t;  
    int a, b, d;  
    int test;  
    while(cin>>n>>m>>s>>t){  
        for(int i=0;i
            scanf("%d", nums+i);  
            dis[i] = MAX;  
        }  
        for(int i=0;i
            for(int j=0;j
                map[i][j] = MAX;  
        for(int i=0;i
            scanf("%d%d%d", &a, &b, &d);  
            map[a][b] = map[b][a] = d;  
        }  
      
        memset(ps, 0, sizeof(ps));  
        dis[s] = 0;  
        ps[s] = nums[s];  
        Node node;  
        node.th = s;  
        node.dis = 0;  
        node.people = nums[s];  
        q.push(node);  
        int ans = 1;  
        while(!q.empty()){  
            Node node  = q.top();  
            q.pop();  
            for(int i=0;i
                if(map[node.th][i]!=MAX){  
                    bool flag = false;  
                    if(dis[node.th]+map[node.th][i]
                        dis[i] = dis[node.th]+map[node.th][i];  
                        ps[i] = ps[node.th] + nums[i];  
                        flag = true;  
                        if(i==t){  
                            ans = 1;  
                        }  
                      
                    }else if(dis[node.th]+map[node.th][i]==dis[i]){  
                          
                        if(i==t){  
                            ans++;    
                        }  
                        if(ps[node.th]+nums[i]>ps[i]){  
                            flag = true;  
                            ps[i] = ps[node.th] + nums[i];  
                        }  
      
                    }  
                    if(flag){  
                        Node tmp;  
                        tmp.th = i;  
                        tmp.dis = dis[i];  
                        tmp.people = ps[i];  
                        q.push(tmp);  
                          
                    }  
                }  
            }  
        }  
  
        printf("%d %d", ans, ps[t]);  
  
    }  
    
    //fclose(stdin);  
    return 0;  
}

좋은 웹페이지 즐겨찾기