码迷,mamicode.com
首页 > 其他好文 > 详细

oj--pat--a1003

时间:2017-06-09 11:12:33      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:algo   can   str   bool   cst   ted   pre   fill   style   

邻接矩阵版。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXV=510;
const int INF=0x7fffffff;
int n,m,st,ed,G[MAXV][MAXV],nweight[MAXV];
int d[MAXV],w[MAXV],num[MAXV];
bool vis[MAXV]={false};

bool Dijkstra(int s){
    fill(d,d+MAXV,INF);
    memset(num,0,sizeof(num));
    memset(w,0,sizeof(w));
    d[s]=0;
    w[s]=nweight[s];
    num[s]=1;
    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(u==-1) return false;/*no-connected--->false */
        
        vis[u]=true;
        for(int v=0;v<n;v++){
            if(vis[v]==false&&G[u][v]!=INF){
                if(d[u]+G[u][v]<d[v]){
                    d[v]=d[u]+G[u][v];
                    w[v]=w[u]+nweight[v];
                    num[v]=num[u];
                }
                else if(d[u]+G[u][v]==d[v]){
                    if(w[u]+nweight[v]>w[v]){
                        w[v]=w[u]+nweight[v];
                    }
                    num[v]+=num[u];
                }
            }
        }
    }
}

int main(){
    scanf("%d %d %d %d",&n,&m,&st,&ed);
    for(int i=0;i<n;i++) scanf("%d",&nweight[i]);
    fill(G[0],G[0]+MAXV*MAXV,INF);
    int tmp1,tmp2;
    for(int i=0;i<m;i++) {
        scanf("%d %d",&tmp1,&tmp2);
        scanf("%d",&G[tmp1][tmp2]);
        G[tmp2][tmp1]=G[tmp1][tmp2];
    }
    bool flag=Dijkstra(st);
    if(flag)
        printf("%d %d\n",num[ed],w[ed]);
    else printf("no");    ///////
    return 0;
}    

 

oj--pat--a1003

标签:algo   can   str   bool   cst   ted   pre   fill   style   

原文地址:http://www.cnblogs.com/kprac/p/6970695.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!