【lightoj-1002】Country Roads(dijkstra变形)

light1002:传送门

【题目大意】

n个点m条边,给一个源点,找出源点到其他点的‘最短路’

定义:找出每条通路中最大的cost,这些最大的cost中找出一个最小的即为‘最短路’,dijkstra变形。dis[i]为s->i的‘最短路’

 1 #include<bits/stdc++.h>
 2 int mp[505][505],dis[505],vis[505];
 3 using namespace std;
 4 int n;
 5 void dij(int s)
 6 {
 7     int i,j,k;
 8     for(i=0; i<n; i++)
 9     {
10         dis[i]=mp[s][i];
11         vis[i]=0;
12     }
13     vis[s]=1;
14     for(j=1; j<n; j++)
15     {
16         int min1=1e9;
17         for(i=0; i<n; i++)
18         {
19             if(min1>dis[i]&&!vis[i])
20             {
21                 min1=dis[i];
22                 k=i;
23             }
24         }
25         vis[k]=1;
26         for(i=0; i<n; i++)
27         {
28             if(!vis[i]&&mp[k][i]<1e9)
29             {
30                 dis[i]=min(dis[i],max(dis[k],mp[k][i]));
31             }
32         }
33     }
34 }
35 int main()
36 {
37     int t,u,v,w,i,j;
38     cin>>t;
39     for(int co=1; co<=t; co++)
40     {
41         int m;
42         scanf("%d%d",&n,&m);
43         for(i=0; i<n; i++)
44             for(j=0; j<n; j++)
45             {
46                 if(i==j)
47                     mp[i][j]=0;
48                 else
49                     mp[i][j]=1e9;
50             }
51         while(m--)
52         {
53             scanf("%d%d%d",&u,&v,&w);
54             if(mp[u][v]>w)
55                 mp[u][v]=mp[v][u]=w;
56         }
57         int s;
58         cin>>s;
59         dij(s);
60         printf("Case %d:\n",co);
61         for(i=0; i<n; i++)
62         {
63             if(dis[i]==1e9)
64                 puts("Impossible");
65             else
66                 printf("%d\n",dis[i]);
67         }
68     }
69     return 0;
70 }

原文地址:https://www.cnblogs.com/lesroad/p/8437899.html

时间: 2024-11-05 12:23:29

【lightoj-1002】Country Roads(dijkstra变形)的相关文章

Light oj 1002 Country Roads (Dijkstra)

题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1002 题目描述: 有n个城市,从0到n-1开始编号,n个城市之间有m条边,中心城市为t,问每个城市到中心城市的最小路径的花费,路径花费大小的定义为:一条路上花费最大的边的值. 解题思路: Dijkstra的变形,用Dijkstra求出来的单源路径可以保证每条边都是最优的,所以最短路上的最长边就是所求. 1 #include <algorithm> 2 #include &

Lightoj 1002 - Country Roads(prim算法)

I am going to my home. There are many cities and many bi-directional roads between them. The cities are numbered from 0 to n-1 and each road has a cost. There are m roads. You are given the number of my city t where I belong. Now from each city you h

【LightOJ 1002】 Country Roads

[LightOJ 1002] Country Roads ...又一个另类OJ...最长路 贴个SPFA的 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue> #include <set> using namespace std; int mp[555][555]; int dis[555]; bool

hdu 3499 Flight dijkstra 变形

Problem Description Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to some other city to avoid meeting her. He will travel only by air and he can go to any city if there exists a flight and it can hel

POJ 2253 Frogger (求每条路径中最大值的最小值,Dijkstra变形)

Frogger Time Limit: 1000MS Memory Limit: 65536K Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tou

HDU1596 find the safest road---(最短路径dijkstra,#变形#)

http://acm.hdu.edu.cn/showproblem.php?pid=1596 分析: 题目要找一条安全度最高的路,安全度计算方法    Safe(P) = s(e1)*s(e2)-*s(ek) e1,e2,ek是P 上的边 在Dijkstra算法的基础上稍加改动 #include "cstdio" #include "cstring" #include "algorithm" using namespace std; #defin

POJ 1797 Heavy Transportation (Dijkstra变形)

F - Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand busines

二 LightOJ 1002 最小生成树

Description I am going to my home. There are many cities and many bi-directional roads between them. The cities are numbered from 0to n-1 and each road has a cost. There are m roads. You are given the number of my city t where I belong. Now from each

POJ 2253 Frogger(Dijkstra变形——最短路径最小权值)

题目链接: http://poj.org/problem?id=2253 Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' suns