HDU 1874 unblocked works continued (Shortest path)

Source: Internet
Author: User

Test instructions: Shortest path

Idea: Shortest Path template

1. Single Source Shortest path (Dijkstra)

#include <iostream>#include<stdio.h>using namespacestd;#defineMAXN 1010#defineTypec int#defineINF 0x3f3f3f3f//prevent back overflow, this can't be too bigBOOLVIS[MAXN];intPRE[MAXN];intCOST[MAXN][MAXN],LOWCOST[MAXN];voidDijkstra (Typec cost[][maxn],typec lowcost[],intNintBeg) {//Beg as the source point    inti,j,k,min;  for(i=0; i<n;++i) {Lowcost[i]=inf;vis[i]=false;p re[i]=-1; } Lowcost[beg]=0;  for(j=0; j<n;++j) {k=-1; Min=INF;  for(i=0; i<n;++i)if(!vis[i]&&lowcost[i]<Min) {Min=Lowcost[i]; K=i; }        if(k==-1) Break; VIS[K]=true;  for(i=0; i<n;++i)if(!vis[i]&&lowcost[k]+cost[k][i]<Lowcost[i]) {Lowcost[i]=lowcost[k]+Cost[k][i]; Pre[i]=K; }    }}intMain () {intN,m;//points, number of sides    inta,b,w,i,j; intS,t;//starting point, end Point     while(~SCANF ("%d%d",&n,&m)) {         for(i=0; i<n;++i) for(j=0; j<n;++j) Cost[i][j]=INF;  for(i=0; i<m;++i) {scanf ("%d%d%d",&a,&b,&W); if(W<cost[a][b]) cost[a][b]=cost[b][a]=w;//} scanf ("%d%d",&s,&t);        Dijkstra (cost,lowcost,n,s); if(Lowcost[t]!=inf) printf ("%d\n", lowcost[t]); Elseprintf"-1\n"); }    return 0;}
View Code

2. Multi-source Shortest path (Floyd)

#include <iostream>#include<stdio.h>using namespacestd;#defineMAXN 1010#defineTypec int#defineINF 0x3f3f3f3f//prevent back overflow, this can't be too bigintPATH[MAXN][MAXN];intCOST[MAXN][MAXN],LOWCOST[MAXN][MAXN];voidFloyd (Typec Cost[][maxn],typec LOWCOST[][MAXN],intN) {    inti,j,k;  for(i=0; i<n;++i) for(j=0; j<n;++j) {Lowcost[i][j]=Cost[i][j]; if(I!=j&&cost[i][j]<inf) path[i][j]=i; Elsepath[i][j]=-1; }     for(k=0; k<n;++k) for(i=0; i<n;++i) for(j=0; j<n;++j)if(lowcost[i][k]+lowcost[k][j]<Lowcost[i][j]) {Lowcost[i][j]=lowcost[i][k]+Lowcost[k][j]; PATH[I][J]=Path[k][j]; }}intMain () {intN,m;//points, number of sides    inta,b,w,i,j; intS,t;//starting point, end Point     while(~SCANF ("%d%d",&n,&m)) {         for(i=0; i<n;++i) for(j=0; j<n;++j) Cost[i][j]=INF;  for(i=0; i<m;++i) {scanf ("%d%d%d",&a,&b,&W); if(W<cost[a][b]) cost[a][b]=cost[b][a]=w;//} scanf ("%d%d",&s,&t);        Floyd (Cost,lowcost,n); if(s==t) printf ("0\n"); Else if(Lowcost[s][t]!=inf) printf ("%d\n", lowcost[s][t]); Elseprintf"-1\n"); }    return 0;}
View Code

HDU 1874 unblocked works continued (Shortest path)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.