zoukankan      html  css  js  c++  java
  • 最短路径———迪杰斯特拉算法

    北大:3767

    I Wanna Go Home
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 2523   Accepted: 1049

    Description

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 #define maxint 999999
     5 int c[610][610],dist[610],p[610];
     6 void dj(int n)
     7 {
     8     int i,j,temp,u,s[610];
     9     for(i=1;i<=n;i++)
    10     {
    11         dist[i]=c[1][i];
    12         s[i]=0;
    13     }
    14     s[1]=1;
    15     dist[1]=0;
    16     for(i=2;i<=n;i++)
    17     {
    18         temp=maxint;
    19         for(j=1;j<=n;j++)
    20             if(!s[j]&&temp>dist[j])
    21             {
    22                 temp=dist[j];
    23                 u=j;
    24             }
    25             s[u]=1;
    26             for(j=1;j<=n;j++)
    27                 if(!s[j]&&dist[j]>dist[u]+c[u][j])
    28                     dist[j]=dist[u]+c[u][j];
    29     }
    30     if(dist[2]==maxint)  printf("-1\n");
    31     else
    32     printf("%d\n",dist[2]);
    33 }
    34 int main()
    35 {
    36     int i,j,x,y,z,n,m;
    37     while(scanf("%d",&n),n)
    38     {
    39         for(i=1;i<=n;i++)
    40             for(j=1;j<=n;j++)
    41                 c[i][j]=maxint;
    42         scanf("%d",&m);
    43         for(i=0;i<m;i++)
    44         {
    45             scanf("%d%d%d",&x,&y,&z);
    46             c[x][y]=z;
    47             c[y][x]=z;
    48         }
    49         for(i=1;i<=n;i++)
    50             scanf("%d",&p[i]);
    51         for(i=1,j=1;i<=n;i++)
    52             for(j=1;j<=n;j++)
    53             {   
    54                 if(p[i]==1&&p[j]==2)
    55                     c[j][i]=maxint;
    56                 else if(p[i]==2&&p[j]==1)
    57                        c[i][j]=maxint;
    58             }
    59     dj(n);
    60     }
    61     return 0;
    62 }
    63     

     

     

    The country is facing a terrible civil war----cities in the country are divided into two parts supporting different leaders. As a merchant, Mr. M does not pay attention to politics but he actually knows the severe situation, and your task is to help him reach home as soon as possible.

    "For the sake of safety,", said Mr.M, "your route should contain at most 1 road which connects two cities of different camp."

    Would you please tell Mr. M at least how long will it take to reach his sweet home?

    Input

    The input contains multiple test cases.

    The first line of each case is an integer N (2<=N<=600), representing the number of cities in the country.

    The second line contains one integer M (0<=M<=10000), which is the number of roads.

    The following M lines are the information of the roads. Each line contains three integers A, B and T, which means the road between city A and city B will cost time T. T is in the range of [1,500].

    Next part contains N integers, which are either 1 or 2. The i-th integer shows the supporting leader of city i.

    To simplify the problem, we assume that Mr. M starts from city 1 and his target is city 2. City 1 always supports leader 1 while city 2 is at the same side of leader 2.

    Note that all roads are bidirectional and there is at most 1 road between two cities.

    Input is ended with a case of N=0.

    Output

    For each test case, output one integer representing the minimum time to reach home.

    If it is impossible to reach home according to Mr. M's demands, output -1 instead.

    Sample Input

    2
    1
    1 2 100
    1 2
    3
    3
    1 2 100
    1 3 40
    2 3 50
    1 2 1
    5
    5
    3 1 200
    5 3 150
    2 5 160
    4 3 170
    4 2 170
    1 2 2 2 1
    0
    

    Sample Output

    100
    90
    540
    
  • 相关阅读:
    WPS表格:统计有效性(即:设为下拉选择框) 及 统计总数
    windows11桌面开始菜单移到左边
    批量将 大量文件的文件名 更为 提前准备好的文件名(一行一个)
    用微信电脑版视频号进行直播(可播放本地视频、窗口画面)
    华为电脑管家、华为手机助手官方下载地址(手机助手app显示空白,可通过重装pc版助手解决)
    《新三国》1080P(89集全)下载
    idea 使用常见问题汇总
    npm切换源
    springboot 与 springcloud 版本匹配
    Nginx学习笔记
  • 原文地址:https://www.cnblogs.com/xiaofanke/p/2650161.html
Copyright © 2011-2022 走看看