P1629 and p1629
Description
There is a postman to deliver, the post office at Node 1. He wants to send N-1 in total, the destination is 2 ~ N. Because the traffic in this city is busy, all the roads are single lines. There are M roads in total. It takes some time to pass through each road. The postman can only carry one thing at a time. How long does it take to deliver this N-1 and finally return to the post office.
Input/Output Format
Input Format:
The first row contains two integers, N and M.
From row 2nd to row M + 1, each line has three numbers U, V, and W, indicating that there is A road from A to B that takes W time. 1 <= U, V <= N, 1 <= W <= 10000. Input ensures that any two points can reach each other.
[Data scale]
For 30% of data, 1 ≤ N ≤ 200;
For 100% of data, 1 ≤ N ≤ 100000, 1 ≤ M ≤.
Output Format:
The output contains only one row and an integer, which is the minimum time required.
Input and Output sample input sample #1:
5 102 3 51 5 53 5 61 2 81 3 85 3 44 1 84 5 33 5 65 4 2
Output sample #1:
83
First, find the shortest length from Point 1 to other points,
Store all edges in reverse order.
Try again
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<queue> 7 #define lli long long int 8 using namespace std; 9 const int MAXN=100001;10 const int maxn=0x7ffff;11 void read(int &n)12 {13 char c='+';int x=0;bool flag=0;14 while(c<'0'||c>'9')15 {c=getchar();if(c=='-')flag=1;}16 while(c>='0'&&c<='9')17 {x=x*10+(c-48);c=getchar();}18 flag==1?n=-x:n=x;19 }20 int n,m;21 struct node22 {23 int u,v,w,nxt;24 }edge[MAXN*4];25 int head[MAXN];26 int num=1;27 int x[MAXN],y[MAXN],z[MAXN];28 void add_edge(int x,int y,int z)29 {30 edge[num].u=x;31 edge[num].v=y;32 edge[num].w=z;33 edge[num].nxt=head[x];34 head[x]=num++;35 }36 int vis[MAXN];37 int dis[MAXN];38 void dj(int bg)39 {40 for(int i=1;i<=n;i++)41 dis[i]=maxn;42 dis[bg]=0;43 queue<int>q;44 memset(vis,0,sizeof(vis));45 q.push(1);46 for(int i=1;i<=n;i++)47 {48 int nowmin=maxn;49 int pos=-1;50 for(int i=1;i<=n;i++)51 {52 if(vis[i]==0&&dis[i]<nowmin)53 {54 pos=i;55 nowmin=dis[i];56 }57 } 58 vis[pos]=1;59 for(int i=head[pos];i!=-1;i=edge[i].nxt)60 {61 int will=edge[i].v;62 if(dis[will]>dis[edge[i].u]+edge[i].w)63 dis[will]=dis[edge[i].u]+edge[i].w;64 }65 }66 }67 int main()68 {69 read(n);read(m);70 for(int i=1;i<=n;i++)71 head[i]=-1;72 for(int i=1;i<=m;i++)73 {74 75 read(x[i]);76 read(y[i]);77 read(z[i]);78 add_edge(x[i],y[i],z[i]);79 }80 int ans=0;81 dj(1);82 int ans2=0;83 for(int i=1;i<=n;i++)84 ans+=dis[i];85 for(int i=1;i<=n;i++)86 head[i]=-1;87 num=1;88 for(int i=1;i<=m;i++)89 add_edge(y[i],x[i],z[i]);90 dj(1);91 for(int i=1;i<=n;i++)92 ans+=dis[i];93 printf("%d",ans);94 return 0;95 }