Happy New Year

Source: Internet
Author: User
There are n stations in Mianyang City, and m two-way roads connect some of them. Each two stations can be connected by at most one road. Any station can be connected by one or more roads to other stations, but different routes may take different time. The time spent on a path is the sum of the time required for all roads on the path. Jia's home is at station 1. He has five relatives, namely, Station A, B, C, D, and E. After the Chinese New Year, he needs to start from his own home, visit every relative (in any order), and send them holiday greetings. How does one take the least time? Input
Line 1: n (n <= 50,000), m (M <= 100,000) indicates the number of stations and highways. Row 2: A, B, C, D, E, number of the station where the five relatives are located (1 <= N ). The following m rows have three integers x, y, t (1 <= X, Y <= N, 1 <= T <= 100 ), number and time of the two stations connected by the road.
Output
Only one row contains an integer T, which is the minimum total time.
Sample Input
6 62 3 4 5 61 2 82 3 33 4 44 5 55 6 21 6 7
Sample output
21
#include<cstdio>#include<algorithm>#include<cstring>#include<new>#include<queue>#include<iostream>using namespace std;int s[7];int dis[7][50001];int n;int m,u,v,w,ans=2e9;bool pg[50001];int flag[7];int ll[7];int ne[300001],fir[50001],val[300001],to[300001],tot;void add(int u,int v,int w){    to[++tot]=v;val[tot]=w;ne[tot]=fir[u];fir[u]=tot;}queue<int> q;int doit(){    int lol=0;    for(int i=1;i<=6;i++)    {        lol+=dis[ll[i]][s[ll[i+1]]];    }    ans=min(lol,ans);}void dfs(int x){    if(x==6) doit();    for(int i=2;i<=6;i++)    {        if(!flag[i])        {            flag[i]=1;            ll[x+1]=i;            dfs(x+1);            flag[i]=0;            ll[x+1]=0;        }    }}int main(){    cin>>n>>m;    s[1]=1;    ll[1]=1;    for(int i=2;i<=6;i++) cin>>s[i];    for(int i=1;i<=m;i++)    {        cin>>u>>v>>w;        add(u,v,w);        add(v,u,w);    }    for(int i=1;i<=6;i++)    {        while(!q.empty())q.pop();        for(int j=1;j<=n;j++) dis[i][j]=1e8;        for(int j=1;j<=n;j++) pg[j]=0;        q.push(s[i]);dis[i][s[i]]=0;pg[s[i]]=1;        while(!q.empty())        {            int xx=q.front();            q.pop();pg[xx]=0;            for(int k=fir[xx];k;k=ne[k])            {                int ttt=to[k];                if(dis[i][xx]+val[k]<=dis[i][ttt])                {                    dis[i][ttt]=dis[i][xx]+val[k];                    if(!pg[ttt])                    {                        q.push(ttt);                        pg[ttt]=1;                    }                }            }        }    }    dfs(1);    cout<<ans<<endl;}

 

Happy New Year

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.