POJ 2387 Til The cows Come Home--Shortest way Dijstra

Source: Internet
Author: User

<span style= "FONT-SIZE:18PX;" > Using Dijstra to Single source the shortest circuit, using Floyd will time out </span>
#include <stdio.h> #include <string.h> #include <algorithm> #define INF 0x3f3f3f3fusing namespace std; const int N = 1005;int map[n][n];int vis[n];int dis[n];int m,n;void dij () {int I,j;memset (vis,0,sizeof (VIS)); for (i=1;i <=n;i++) {if (i==1) Dis[i]=0;else dis[i]=map[1][i];} Vis[1]=1;for (i=2;i<=n;i++) {int m=inf,t;for (j=1;j<=n;j++) {if (!vis[j]&&dis[j]<m) {t=j;m=dis[j];}} Vis[t]=1;for (j=1;j<=n;j++) {if (!vis[j]&&dis[j]>dis[t]+map[t][j]) dis[j]=dis[t]+map[t][j];}}} int main () {int i,j;while (~SCANF ("%d%d", &m,&n)) {memset (map,inf,sizeof (map)); for (i=1;i<=m;i++) {int x, y, Z ; scanf ("%d%d%d", &x,&y,&z), Map[x][y]=min (map[x][y],z); Map[y][x]=min (map[y][x],z);} Dij ();p rintf ("%d\n", Dis[n]);} return 0;}


Poj 2387 Til The cows Come Home--Shortest way Dijstra

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.