90荤,不知道还有10分去哪了,未解之谜
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define INF 0xfffffff
#define maxn 1002
long long dist[maxn],sum[maxn];
struct Edge{
int e,w,t;
};
struct Node
{
int e;
int w;
friend bool operator < (Node A, Node B)
{
return A.w > B.w;
}
};
bool vis[maxn];
int m, n;
vector< vector<Edge> > G;
long long Dij(int Star,int End)
{
Node P, Pn;
P.e = Star;
P.w = 0;
sum[0]=sum[1]=0;
priority_queue<Node> Q;
dist[0]=dist[1]=0;
int i;
for(i=2;i<=n;i++)
dist[i]=INF;
Q.push(P);
while( !Q.empty() )
{
P = Q.top();
Q.pop();
if( vis[P.e] )
continue;
vis[P.e] = true;
if( P.e == End )
return P.w;
int len = G[P.e].size();
for(int i=0; i< len; i++)
{
long long cost;
if(G[P.e][i].t)
{
long long temp=sum[P.e]+G[P.e][i].w;
cost=dist[P.e]-sum[P.e]*sum[P.e]+temp*temp;
}
else {
sum[G[P.e][i].e]=0;
cost=dist[P.e]+G[P.e][i].w;
}
if(cost<dist[G[P.e][i].e]) {
dist[G[P.e][i].e]=cost;
if(G[P.e][i].t) sum[G[P.e][i].e]=sum[P.e]+G[P.e][i].w;
}
Pn.e = G[P.e][i].e;
Pn.w = dist[Pn.e];
if( !vis[Pn.e] )
Q.push(Pn);
}
}
return -1;
}
int main()
{
Edge P;
cin >> n >> m;
G.clear();
G.resize(n+1);
memset(vis,false,sizeof(vis));
for(int i=0; i<m; i++)
{
int a, b, c,k;
cin >> k>>a >> b >> c;
P.e = b;
P.w = c;
P.t=k;
G[a].push_back(P);
P.e = a;
G[b].push_back(P);
}
long long ans = Dij(1,n);
cout << ans << endl;
return 0;
}