# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
61741 | ansol4328 | 주유소 (KOI16_gas) | C++98 | 1440 ms | 66560 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<stdio.h>
#include<vector>
#include<queue>
#include<memory.h>
using namespace std;
typedef pair<long long,long long> P;
int n, gas[2502];
vector<P> lst[2502];
priority_queue<P, vector<P>, greater<P> > PQ;
long long dxy[2502][2502];
void make_dxy(int s)
{
int v;
bool check[2502];
for(int i=1 ; i<=n ; i++) dxy[s][i]=1e18;
dxy[s][s]=0;
memset(check,false,sizeof(check));
PQ.push(P(0,s));
while(!PQ.empty())
{
do
{
v=PQ.top().second;
PQ.pop();
}while(!PQ.empty() && check[v]);
check[v]=true;
for(int i=0 ; i<lst[v].size() ; i++)
{
if(dxy[s][lst[v][i].first]>dxy[s][v]+lst[v][i].second*gas[s] && !check[lst[v][i].first])
{
dxy[s][lst[v][i].first]=dxy[s][v]+lst[v][i].second*gas[s];
PQ.push(P(dxy[s][lst[v][i].first],lst[v][i].first));
}
}
}
}
long long dijkstra()
{
int v;
long long dist[2502];
bool check[2502];
for(int i=1 ; i<=n ; i++) dist[i]=1e18;
dist[1]=0;
memset(check,false,sizeof(check));
PQ.push(P(0,1));
while(!PQ.empty())
{
do
{
v=PQ.top().second;
PQ.pop();
}while(!PQ.empty() && check[v]);
if(v==n) break;
check[v]=true;
for(int i=0 ; i<lst[v].size() ; i++)
{
if(dist[lst[v][i].first]>dist[v]+lst[v][i].second && !check[lst[v][i].first])
{
dist[lst[v][i].first]=dist[v]+lst[v][i].second;
PQ.push(P(dist[lst[v][i].first],lst[v][i].first));
}
}
}
return dist[n];
}
int main()
{
int m, a, b, c;
scanf("%d %d",&n,&m);
for(int i=1 ; i<=n ; i++) scanf("%d",&gas[i]);
for(int i=0 ; i<m ; i++)
{
scanf("%d %d %d",&a,&b,&c);
lst[a].push_back(P(b,c));
lst[b].push_back(P(a,c));
}
for(int i=1 ; i<=n ; i++) make_dxy(i);
for(int i=1 ; i<=n ; i++)
{
lst[i].clear();
for(int j=1 ; j<=n ; j++)
{
if(i!=j) lst[i].push_back(P(j,dxy[i][j]));
}
}
printf("%lld",dijkstra());
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |