The Beautiful Path

来源:互联网 发布:天刀男性捏脸数据导入 编辑:程序博客网 时间:2024/06/06 06:30
The Beautiful PathTime Limit: 10000ms, Special Time Limit:25000ms, Memory Limit:65536KBTotal submit users: 10, Accepted users: 1Problem 13771 : No special judgementProblem descriptionThere is an undirected graph with n nodes and m edges.There maybe have multipul edges and selfcycle.Now there is a trip from node a to node b.XiaoMing want to know how much time it will take at least ,and in this case , how many edges he will pass at least.InputThere are lots of case.
For each test case.Only one line contains four integers n,m(n<=100000, 1<=m<=200000)a,b ( a<= n b<=n ), which indicates the number of nodes and number of edges, the start node and the end node.
The next m lines describe the graph.OutputFor each test case, you output the minimul time and the minimul edge nums in one line “Case #%d: %d %d” Sample Input
10 20 4 71 2 82 3 41 4 72 5 103 6 32 7 11 8 43 9 12 10 38 9 21 3 108 4 33 10 610 9 37 4 810 3 210 2 49 8 51 6 52 7 4
Sample Output
Case #1: 8 1
Problem Source

The 13th Programming Contest ofHunan University, 2017

#include<iostream>#include<stdio.h>#include<string.h>#include<vector>#include<queue>using namespace std;const int inf=0x3f3f3f3f;int n,m,s,e;struct edge{    int v,w;    edge(int V,int W){        v=V,w=W;    }    edge(){}};vector<edge> u[100005];queue<int> q;bool vis[100005];int dis[100005],sum[100005];void spfa(){    for(int i=1;i<=n;i++) vis[i]=0,dis[i]=inf;    q.push(s);vis[s]=1,dis[s]=0;    while(!q.empty()){        int uu=q.front();q.pop();        vis[uu]=0;        int siz=u[uu].size();        for(int i=0;i<siz;i++){            int vv=u[uu][i].v,ww=u[uu][i].w;            if(dis[vv]>dis[uu]+ww){                dis[vv]=dis[uu]+ww;                if(!vis[vv]) vis[vv]=1,q.push(vv);            }        }    }}void bfs(){    for(int i=1;i<=n;i++) vis[i]=0,sum[i]=0;    q.push(s);vis[s]=1;    while(!q.empty()){        int uu=q.front();q.pop();        vis[uu]=0;        int siz=u[uu].size();        for(int i=0;i<siz;i++){            int vv=u[uu][i].v,ww=u[uu][i].w;            if(dis[vv]==dis[uu]+ww){               sum[vv]=sum[uu]+1;               if(!vis[vv]){                  vis[vv]=1;                  q.push(vv);               }            }        }    }}int main(){    int cas=0;    while(scanf("%d%d%d%d",&n,&m,&s,&e)!=EOF){        for(int i=1;i<=n;i++) u[i].clear();        for(int uu,vv,ww,i=0;i<m;i++){            scanf("%d%d%d",&uu,&vv,&ww);            u[uu].push_back(edge(vv,ww));            u[vv].push_back(edge(uu,ww));        }        spfa();        bfs();        printf("Case #%d: %d %d\n",++cas,dis[e],sum[e]);    }}

                                             
0 0