-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathorient.cpp
62 lines (62 loc) · 1.21 KB
/
orient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# include <fstream>
# include <vector>
# include <queue>
# define inf 99999999;
using namespace std;
ifstream f("orient.in");
ofstream g("orient.out");
struct elem
{
int x,y;
}muchie[3005];
vector <int> v[1005];
queue <int> q;
int i,j,n,m,x,y,tata,c,minn;
int cost[1005][1005];
int CC[1005];
void init ()
{
for (int i=1; i<=n; ++i)
CC[i]=inf;
}
void BF (int k, int x)
{
int i,X=k,Y=x;
CC[k]=0;
q.push(k);
while (! q.empty())
{
k=q.front(); q.pop();
if (CC[k]>CC[x]) continue;
for (i=0; i<v[k].size(); ++i)
{
if (k==X && v[k][i]==x) continue;
if (CC[k]+cost[k][v[k][i]]<CC[v[k][i]])
{
CC[v[k][i]]=CC[k]+cost[k][v[k][i]];
q.push(v[k][i]);
}
}
}
}
int main ()
{
f>>n>>m;
for (i=1; i<=m; ++i)
{
f>>x>>y>>c;
muchie[i].x=x; muchie[i].y=y;
cost[x][y]=0; cost[y][x]=c;
v[x].push_back(y);
v[y].push_back(x);
}
minn=inf;
for (i=1; i<=m; ++i)
{
init ();
BF (muchie[i].y, muchie[i].x);
if (CC[muchie[i].x]+cost[x][y]<minn) minn=CC[muchie[i].x]+cost[x][y];
}
g<<minn<<"\n";
return 0;
}