-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10449.cpp
112 lines (104 loc) · 1.75 KB
/
10449.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
10449 - Traffic
*/
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e8+9;
struct node
{
int a,b,cost;
} graph[2020];
int cost[2020];
int dist[2020];
int n,m;
void bellman_ford()
{
for(int i=0; i<=210; i++)
{
dist[i]=INF;
}
dist[1]=0;
for(int i=0; i<n-1; i++)
{
bool update =false;
for(int j=1; j<=m; j++)
{
int u = graph[j].a;
int v = graph[j].b;
if(dist[u]+graph[j].cost<dist[v] && dist[u]!=INF)
{
dist[v]=dist[u]+graph[j].cost;
update=true;
}
}
if(update==false)
break;
}
///checking for negative cycle
for(int j=1; j<=m; j++)
{
int u = graph[j].a;
int v = graph[j].b;
if(dist[u]+graph[j].cost<dist[v] && dist[u]!=INF)
{
dist[v]=-INF;
}
}
int q,in;
cin>>q;
for(int i=1; i<=q; i++)
{
cin>>in;
if(dist[i]==INF || dist[i]<3)
{
cout<<"?"<<endl;
}
else
{
cout<<dist[in]<<endl;
}
}
}
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int tc=0,u,v,c;
while(cin>>n)
{
for(int i=1; i<=n; i++)
{
cin>>cost[i];
}
cin>>m;
for(int i=1; i<=m; i++)
{
cin>>u>>v;
int temp = (cost[v]-cost[u])*(cost[v]-cost[u])*(cost[v]-cost[u]);
graph[i].a=u;
graph[i].b=v;
graph[i].cost=temp;
}
cout<<"Set #"<<(++tc)<<endl;
bellman_ford();
}
return 0;
}
/*
Sample Input
5 6 7 8 9 10
6
1 2
2 3
3 4
1 5
5 4
4 5
2
4
5
Sample Output
Set #1
3
4
*/