1003. Emergency (25)

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
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 510; const int INF = 1000000000; int n,m,st,ed;//城市数,道路数,出发城市,目的城市 int G[maxn][maxn],num[maxn];//邻接矩阵,最短路径条数 int weight[maxn],w[maxn],d[maxn];//点权,最大权之和 ,最短路径 bool vis[maxn] = {false};//是否访问过 void Dijkstra(int s){ fill(d,d+maxn,INF); memset(w,0,sizeof(w)); memset(num,0,sizeof(num)); d[s] = 0; num[s] = 1; w[s] = weight[s]; for(int i = 0 ; i < n ; i++){ int u = -1, MIN = INF; for(int j = 0 ; j < n ; j++){ if(vis[j] == false && d[j] < MIN){ u = j; MIN = d[j]; }//if }//for(j) if(u == -1) return; vis[u] = true; for(int v = 0 ; v < n ; v++){ if(vis[v] == false && G[u][v] != INF){ if(d[v] > d[u] + G[u][v]){ d[v] = d[u] + G[u][v]; w[v] = w[u] + weight[v]; num[v] = num[u]; //如果到达v点经过u点距离更近的话,通往v的最短路径就是通往u的最短路径 }else if(d[v] == d[u]+G[u][v]){ if(w[v] < w[u]+weight[v]){ w[v] = w[u]+weight[v]; }//else num[v] += num[u]; // 通往v路径的距离和经过u的路径距离一样,无论权重是否需要更换,最短路径条数都需要加上num【u】 }//else if }//if } //for }//for int } int main(){ scanf("%d%d%d%d",&n,&m,&st,&ed); for(int i = 0;i < n;i++){ scanf("%d",&weight[i]); } int u,v; fill(G[0],G[0]+maxn*maxn,INF); for(int i = 0 ;i < m;i++){ scanf("%d%d",&u,&v); scanf("%d",&G[u][v]); G[v][u] = G[u][v]; } Dijkstra(st); printf("%d %d\n",num[ed],w[ed]); return 0; }

 

//Bellman
#include<cstdio>
#include<set> 
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std; //不能忘 
const int maxn = 510;
const int INF = 0x3fffffff;

struct node{   //没有() 
    int v,dis;
    node(int _v, int _dis):v(_v),dis(_dis){}
};
vector<node> Adj[maxn]; //边权矩阵 
int d[maxn],w[maxn],num[maxn];//最短距离,最大点权之和,最短路径条数 
int weight[maxn]; //点权 
int n,m,st,ed; 
set<int> pre[maxn];


void Bellman(int s){
    fill(d,d+maxn,INF);
    memset(num,0,sizeof(num));
    memset(w,0,sizeof(w));
    d[s] = 0;
    num[s] = 1;
    w[s] = weight[s];
    for(int i = 0; i < n-1; i++){  //n个顶点,n-1条边 
        for(int u = 0; u < n; u++){   //遍历每条边,所有顶点 
            for(int j = 0; j < Adj[u].size(); j++){
                int v = Adj[u][j].v;
                int dis = Adj[u][j].dis;
                if(d[v] > d[u] + dis){
                    d[v] = d[u] + dis;
                    w[v] = w[u] + weight[v];
                    num[v] = num[u];
                    pre[v].clear();
                    pre[v].insert(u);  //set集合是插入操作 
                }else if(d[v] == d[u] + dis){
                    if(w[v] < w[u] + weight[v]){
                        w[v] = w[u] + weight[v];
                    }
                    pre[v].insert(u);
                    num[v] = 0;
                    set<int>::iterator it;
                    for(it = pre[v].begin(); it != pre[v].end(); it++){
                        num[v] += num[*it];
                    }
                }
            }
        }
    } 
}


int main(){
    scanf("%d%d%d%d",&n,&m,&st,&ed);
    for(int i = 0; i < n; i++){
        scanf("%d",&weight[i]);
    } 
    int u,v,wt;//wt为临时边权 ,node里的dis 
    for(i = 0; i < m; i++){
        scanf("%d%d%d",&u,&v,&wt);
        Adj[u].push_back(node(v,wt));
        Adj[v].push_back(node(u,wt));
    } 
    Bellman(st);
    printf("%d %d\n",num[ed],w[ed]);
    return 0;
}

 

posted @ 2018-03-07 18:08  王清河  阅读(163)  评论(0编辑  收藏  举报