Spfa twice. Calculates the sum of the least short circuit between the come and the return.
Using Dijkstra + adjacent matrix is indeed easy to write + easy to exchange, but there are 1000000 points, and the matrix cannot be opened.
D1 [] is 1 ~ N.
Swap the neighboring points of all edges.
D2 [] is 1 ~ N.
All are added as the desired answer.
Sadly, spfa "HDU 1535" AC is used, but poj 1511 is the same as poj.
Then the obsessive-compulsive disorder keeps testing.
Prices are positive integers the sum of which is smaller than 1000000000
Int can be used. HDU is like this.
Then I changed the sum of poj to long. Or wa.
Then I found that there was a problem with INF, and 0 xfffffff was not enough. Then change it to the maximum value of 0x7fffff int, AC.
Poj data is awesome. I do not want to read the question at all.
#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<queue>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<cmath>#define INF 0x7fffffff#define eps 1e-6using namespace std;int n,m;struct lx{ int v,d;};int dis[1000001];bool vis[1000001];int e[1000001];vector<lx>g[1000001];void swapg(){ for(int i=1;i<=n;i++) e[i]=g[i].size(); for(int i=1;i<=n;i++) { int u,v,d; lx now; u=i; for(int j=0;j<e[i];j++) { v=g[u][j].v,d=g[u][j].d; now.d=d,now.v=u; g[v].push_back(now); } }}int SPFA(int thend) // long long{ for(int i=1;i<=n;i++) dis[i]=INF,vis[i]=0; queue<int>q; dis[1]=0,vis[1]=1; q.push(1); while(!q.empty()) { int u=q.front();q.pop(); vis[u]=0; for(int j=e[u];j<g[u].size();j++) { int v=g[u][j].v; int d=g[u][j].d; if(dis[v]>dis[u]+d) { dis[v]=dis[u]+d; if(!vis[v]) { vis[v]=1; q.push(v); } } } } int ans=0; //long long for(int i=1;i<=n;i++) ans+=dis[i]; return ans;}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); int u,v,d; lx now; for(int i=1;i<=n;i++) g[i].clear(); while(m--) { scanf("%d%d%d",&u,&v,&d); now.d=d; now.v=v; g[u].push_back(now); } memset(e,0,sizeof(e)); int dis1=SPFA(n); //long long swapg(); int dis2=SPFA(n); //long long printf("%d\n",dis1+dis2); // lld% }}