CCF 交通規劃 最簡單的解答
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct edge
{
int to,cost;
edge(int a,int b)
{
to=a;
cost=b;
}
};
vector<edge> point[10001];
long D[10001];
long INF = 100000001;
long minuse[10001];
int main()
{
long n,m;
cin>>n>>m;
int a,b,c;
for(long i=0;i<m;i++)
{
cin>>a>>b>>c;
point[a].push_back(edge(b,c));
point[b].push_back(edge(a,c));
}
for(int i=1;i<=n;i++)
D[i]=INF;
D[0]=D[1]=0;
for(int i=1;i<=n;i++)
minuse[i]=INF;
minuse[0]=minuse[1]=0;
vector<int> s;
vector<int> q;
for(int i=1;i<=n;i++)
q.push_back(i);
long minflag=INF;
int v;
while(q.size()!=0)
{
minflag=INF;
for(int i=0;i<q.size();i++)
{
if(D[q[i]]<minflag)
{
minflag=D[q[i]];
v=i;
}
}
s.push_back(q[v]);
int j=q[v];
q.erase(q.begin()+v);
for(long i=0;i<point[j].size();i++)
{
if(D[point[j][i].to]>D[j]+point[j][i].cost)
{
D[point[j][i].to]=D[j]+point[j][i].cost;
minuse[point[j][i].to]=point[j][i].cost;
}
else
if(D[point[j][i].to]==D[j]+point[j][i].cost&&minuse[point[j][i].to]>point[j][i].cost)
minuse[point[j][i].to]=point[j][i].cost;
else;
}
}
long pp=0;
for(int i=1;i<=n;i++){
pp = pp+minuse[i];
}
cout<<pp;
return 0;
}