Skip to content

Commit 6cb3d9c

Browse files
利用Floyd-Warshall algorithm解決類似shortest path 的問題
1 parent 6e1a537 commit 6cb3d9c

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

uva/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
### 圖論
3737
[[a674]](https://zerojudge.tw/ShowProblem?problemid=a674)[[解答]](https://github.com/XassassinXsaberX/zerojudge/blob/master/uva/a674.c) (Floyd-Warshall algorithm)
38+
[[c128]](https://zerojudge.tw/ShowProblem?problemid=c128)[[解答]](https://github.com/XassassinXsaberX/zerojudge/blob/master/uva/c128.cpp) (Floyd-Warshall algorithm)
3839
[[d282]](https://zerojudge.tw/ShowProblem?problemid=d282)[[解答]](https://github.com/XassassinXsaberX/zerojudge/blob/master/uva/d282.c) (shortest path + Floyd-Warshall algorithm)
3940
[[d792]](https://zerojudge.tw/ShowProblem?problemid=d792)[[解答]](https://github.com/XassassinXsaberX/zerojudge/blob/master/uva/d792.c) (shortest path + Floyd-Warshall algorithm)
4041
[[d793]](https://zerojudge.tw/ShowProblem?problemid=d793)[[解答]](https://github.com/XassassinXsaberX/zerojudge/blob/master/uva/d793.cpp) (shortest path + dijkstra algorithm)

uva/c128.cpp

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#include <iostream>
2+
#include <map>
3+
#include <stdlib.h>
4+
#define INFINITE 100000000
5+
#define min(a,b) ((a)<(b))?(a):(b)
6+
7+
/*利用Floyd-Warshall algorithm解決類似shortest path 的問題*/
8+
9+
using namespace std;
10+
void floyd(int**graph,int n);
11+
12+
13+
int main()
14+
{
15+
int n,r;
16+
map<string,int> MAP; //用來將字串轉換成數字
17+
map<string,int>::iterator iter,iter1,iter2;
18+
string city1,city2;
19+
int index,index1,index2;
20+
int weight;
21+
int**graph,*ptr;
22+
int i,j,k;
23+
int c=1;
24+
int ans;
25+
26+
while(cin>>n>>r)
27+
{
28+
if(n==0 && r==0)
29+
break;
30+
//動態分配二維陣列
31+
graph = new int*[n];
32+
for(i=0;i<n;i++)
33+
graph[i] = new int[n];
34+
35+
//接下來初始化圖型
36+
for(i=0;i<n;i++)
37+
for(j=0;j<n;j++)
38+
{
39+
if(i == j)
40+
graph[i][j] = 0;
41+
else
42+
graph[i][j] = -INFINITE;
43+
}
44+
45+
46+
index = 0 ;//用來代表每個城市的代號
47+
48+
//以下開始建立圖形的權重
49+
for(i=0;i<r;i++)
50+
{
51+
cin>>city1>>city2>>weight;
52+
53+
54+
//尋找city1的編號
55+
iter = MAP.find(city1);
56+
if(iter == MAP.end())
57+
{
58+
index1 = index; //代表city1 為標號 index1
59+
MAP.insert(pair<string,int>(city1,index++)); //將city1 與該編號丟到map容器中
60+
}
61+
else
62+
index1 = iter->second; //代表city1 為編號 index1
63+
64+
//尋找city2的編號
65+
iter = MAP.find(city2);
66+
if(iter == MAP.end())
67+
{
68+
index2 = index; //代表city2 為標號 index12
69+
MAP.insert(pair<string,int>(city2,index++)); //將city2 與該編號丟到map容器中
70+
}
71+
else
72+
index2 = iter->second; //代表city1為編號 index1
73+
74+
graph[index1][index2] = weight;
75+
graph[index2][index1] = weight;
76+
77+
}
78+
79+
cin>>city1>>city2;
80+
81+
82+
//尋找city1的編號
83+
iter = MAP.find(city1);
84+
index1 = iter->second; //代表city1 為編號 index1
85+
86+
//尋找city2的編號
87+
iter = MAP.find(city2);
88+
index2 = iter->second; //代表city2 為編號 index2
89+
90+
//利用Floyd-Warshall algorithm解決類似shortest path 的問題
91+
floyd(graph,n);
92+
93+
cout<<"Scenario #"<<c++<<endl;
94+
ans = min(graph[index1][index2],graph[index2][index1]);
95+
cout<< ans <<" tons"<<endl;
96+
97+
98+
99+
//釋放二維陣列空間
100+
for(i=0;i<n;i++)
101+
delete [] graph[i];
102+
delete [] graph;
103+
MAP.clear();
104+
}
105+
return 0;
106+
107+
}
108+
109+
110+
void floyd(int**graph,int n)
111+
{
112+
int i,j,k;
113+
int min_value;
114+
for(k=0;k<n;k++)
115+
for(i=0;i<n;i++)
116+
for(j=0;j<n;j++)
117+
{
118+
min_value = min(graph[i][k],graph[k][j]);
119+
if(min_value > graph[i][j])
120+
graph[i][j] = min_value;
121+
}
122+
}
123+
124+
125+
126+
127+
128+
129+
130+

0 commit comments

Comments
 (0)