城市平乱

来源:互联网 发布:数控冲床编程招聘 编辑:程序博客网 时间:2024/05/01 22:29

城市平乱

dijkstra模板题.

//// Created by luozujian on 17-10-17.//#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<queue>#include<vector>#define INF 0x3f3f3f3fusing namespace std;const int maxv = 1e3+5;const int maxp = 1e2+5;typedef pair<int,int> P;struct edge{    int to,cost;};vector<edge>G[maxv];int d[maxv];int a[maxp];int n,m,p,q;void dijkstra(int v){    priority_queue<P,vector<P>,greater<P> >que;    fill(d,d+m+1,INF);    d[v] = 0;    que.push(P(0,v));    while(que.size())    {        P p = que.top();que.pop();        int v = p.second;        if(d[v] < p.first) continue;        for(int i=0;i<G[v].size();i++)        {            edge e = G[v][i];            if(d[e.to] > d[v] + e.cost)            {                d[e.to] = d[v] + e.cost;                que.push(P(d[e.to],e.to));            }        }    }}void solve(){    dijkstra(q);    int ans = INF;    for(int i=1;i<=n;i++)    {        ans = min(ans,d[a[i]]);    }    printf("%d\n",ans);}int main(){    int t;    scanf("%d",&t);    while(t--)    {        for(int i=0;i<maxv;i++)G[i].clear(); //一定要记得清零        scanf("%d%d%d%d",&n,&m,&p,&q);        for(int i=1;i<=n;i++) scanf("%d",&a[i]);        for(int i=0;i<p;i++)        {            int s,t,cost;            scanf("%d%d%d",&s,&t,&cost);            edge e;            e.to = t;            e.cost = cost;            G[s].push_back(e);            e.to = s;  //无向图            e.cost = cost;            G[t].push_back(e);        }        solve();    }    return 0;}
原创粉丝点击