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

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
  1. #include  
  2. #include  
  3. #include  
  4. #include  
  5. #include  
  6. #include  
  7. #include  
  8. #include  
  9. using namespace std;  
  10. #define MAX 0x6FFFFFFF  
  11. int nums[501];  
  12. int map[501][501];  
  13. int dis[501];  
  14. int ps[501];  
  15. struct Node{  
  16.     int dis;  
  17.     int people;  
  18.     int th;  
  19.     bool operator<(const Node& node)const{  
  20.         if(dis!=node.dis)  
  21.             return dis
  22.         return people>node.people;  
  23.     }  
  24. };  
  25. priority_queue q;  
  26. int main(){  
  27.   
  28.     //freopen("in.txt", "r", stdin);  
  29.   
  30.     int n, m;  
  31.     int s, t;  
  32.     int a, b, d;  
  33.     int test;  
  34.     while(cin>>n>>m>>s>>t){  
  35.         for(int i=0;i
  36.             scanf("%d", nums+i);  
  37.             dis[i] = MAX;  
  38.         }  
  39.         for(int i=0;i
  40.             for(int j=0;j
  41.                 map[i][j] = MAX;  
  42.         for(int i=0;i
  43.             scanf("%d%d%d", &a, &b, &d);  
  44.             map[a][b] = map[b][a] = d;  
  45.         }  
  46.       
  47.         memset(ps, 0, sizeof(ps));  
  48.         dis[s] = 0;  
  49.         ps[s] = nums[s];  
  50.         Node node;  
  51.         node.th = s;  
  52.         node.dis = 0;  
  53.         node.people = nums[s];  
  54.         q.push(node);  
  55.         int ans = 1;  
  56.         while(!q.empty()){  
  57.             Node node  = q.top();  
  58.             q.pop();  
  59.             for(int i=0;i
  60.                 if(map[node.th][i]!=MAX){  
  61.                     bool flag = false;  
  62.                     if(dis[node.th]+map[node.th][i]
  63.                         dis[i] = dis[node.th]+map[node.th][i];  
  64.                         ps[i] = ps[node.th] + nums[i];  
  65.                         flag = true;  
  66.                         if(i==t){  
  67.                             ans = 1;  
  68.                         }  
  69.                       
  70.                     }else if(dis[node.th]+map[node.th][i]==dis[i]){  
  71.                           
  72.                         if(i==t){  
  73.                             ans++;    
  74.                         }  
  75.                         if(ps[node.th]+nums[i]>ps[i]){  
  76.                             flag = true;  
  77.                             ps[i] = ps[node.th] + nums[i];  
  78.                         }  
  79.       
  80.                     }  
  81.                     if(flag){  
  82.                         Node tmp;  
  83.                         tmp.th = i;  
  84.                         tmp.dis = dis[i];  
  85.                         tmp.people = ps[i];  
  86.                         q.push(tmp);  
  87.                           
  88.                     }  
  89.                 }  
  90.             }  
  91.         }  
  92.   
  93.         printf("%d %d\n", ans, ps[t]);  
  94.   
  95.     }  
  96.     
  97.     //fclose(stdin);  
  98.     return 0;  
  99. }
     

你可能感兴趣的:(PAT)