1. 程式人生 > >csp201712-4 行車路線

csp201712-4 行車路線

#include<iostream>
using namespace std;
#include<queue>
#define LL long long
#define maxn 510
const int inf=1e9+7
const int maxm=1e5+10;
int n,m,arr[maxn],cnt,vis[maxn];
LL d[maxn],s[maxn];

struct edge
{
    int v,val,type,next;
}e[maxm*4];

struct node
{
    int v;
    LL dis,pre;
    node(int
a,LL b,LL c) { v=a;dis=b;pre=c; } bool operator<(const node& p)const { if(dis==p.dis) return pre>p.pre; return dis>p.dis; } }; void init() { cnt=0; for(int i=1;i<2*n+1;i++) { arr[i]=-1; d[i]=inf; if
(i<=n)s[i]=0; else s[i]=inf; vis[i]=0; } } void add(int u,int v,int type,int val) { e[cnt].val=val; e[cnt].v=v; e[cnt].type=type; e[cnt].next=arr[u]; arr[u]=cnt++; } LL dj(int in,int t) { priority_queue<node> q; d[in]=s[in]=0; q.push(node(in,d[in
],s[in])); d[in+n]=s[in+n]=0; q.push(node(in+n,d[in+n],s[in+n])); while(!q.empty()) { node tmp=q.top(); q.pop(); int u=tmp.v; if(vis[u])continue; vis[u]=1; for(int i=arr[u];i+1;i=e[i].next) { int v=e[i].v; if(vis[v])continue; if(e[i].type) {//small road LL len=d[u]+2*(LL)e[i].val*s[u]+(LL)e[i].val*(LL)e[i].val; if(d[v]>len||(d[v]==len&&s[v]>s[u]+e[i].val)) { d[v]=len; s[v]=s[u]+e[i].val; q.push(node(v,d[v],s[v])); } } else {//big road if(d[v]>d[u]+e[i].val) { d[v]=d[u]+e[i].val; q.push(node(v,d[v],s[v])); } } } } return min(d[t],d[n+t]); } int main() { cin>>n>>m; init(); while(m--) { int t,a,b,c; scanf("%d%d%d%d",&t,&a,&b,&c); add(a,b+n*t,t,c); //a,b,0,c a,b+n,1,c add(a+n,b+n*t,t,c);//a+n,b,0,c a+n,b+n,1,c add(b,a+n*t,t,c);//b,a,0,c b,a+n,1,c add(b+n,a+n*t,t,c);//b+n,a,0,c b+n,a+n,1,c } printf("%lld\n",dj(1,n)); }