Bzoj 1880: [Sdoi2009]elaxia's route (topology sort +SPFA)

Source: Internet
Author: User

1880: [Sdoi2009]elaxia's route time limit: 4 Sec Memory Limit: + MB
Submit: 974 Solved: 382
[Submit] [Status] [Discuss] Description recently, the relationship between Elaxia and w** was particularly good, and they wanted to be together all day long, but the university was so nervous that they had to reasonably arrange for two people to spend time with each other. Elaxia and w** every day to travel between the dorm and the laboratory, they want to save time under the premise of walking together as long as possible. It is now known that the number of dormitories and laboratories where Elaxia and w** are located, as well as the school map: There are n intersections on the map, M roads, and it takes a certain amount of time to pass each route. Specifically, the longest common path between two pairs of points is required in a non-aligned graph. Input first line: two integers n and m (meaning as described in the title). The second line: four integers x1, y1, x2, y2 (1≤x1≤n,1≤y1≤n,1≤x2≤n,1≤≤n), respectively, representing Elaxia dormitories and laboratory and w** quarters and laboratory markings (two pairs of points x1,y1 and X2,y2 respectively). Next m line: three integers per line, u, V, L (1≤u≤n,1≤v≤n,1≤l≤10000), there is a road between table U and V, the time required to pass this road is L. Out-of-Gegger::: A line, an integer representing the time of two people per day (that is, the length of the longest public path). An output row, an integer that represents the time (that is, the length of the longest public path) of two people per day, Sample Input9 10
1 6 7 8
1 2 1
2 5 2
2 3 3
5 | |
3 9 5
4 5 3
4 6 4
4 7 2
5 8 1
7 9 1
Sample Output3HINT

For 30% of data, n≤100;
For 60% of data, n≤1000;
For 100% of the data, n≤1500, the input data is guaranteed to have no heavy edges and self-loops.

Source

Day2

[Submit] [Status] [Discuss] Puzzle : topological sort +SPFA

take the s1,t1,s2,t2 as the source point to run the SPFA, record the single source shortest, and then through the way of F[u]+len[u,v]+g[v]=len all on the two people to join the common side of the minimum road map, and then use topological sorting to find the longest chain is answer.

Since there may be a path direction problem, all we have to do is turn the s2,t2 back to the other side.

Attached Sample group: (Can be stuck off some AC programs)

8 6

1 7 3 8

1 2 1

1 4 1

4 8 1

2 3 1

2 7 3

4 7 3 Output:1

Some programs change 1 7 3 8 to 3 8 1 7 The answer will be different.

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < queue> #include <cmath> #define N 2000using namespace Std;int n,m,s1,t1,s2,t2,len1,len2,ans;int f1[n],g1[n],f2 [N],g2[n],can[n],ins[n],q[n];int point[n],next[1000000],v[1000000],len[1000000],tot,cnt;int point1[N],next1[ 1000000],v1[1000000],c[1000000];int maxn[n],f[n];void Insert (int x,int y,int z) {tot++; next[tot]=point[x]; point[x]= Tot V[tot]=y; len[tot]=z;tot++; Next[tot]=point[y]; Point[y]=tot; V[tot]=x; Len[tot]=z;} void Add (int x,int y,int z) {cnt++; next1[cnt]=point1[x]; point1[x]=cnt; v1[cnt]=y; c[cnt]=z;} void SPFA (int *dis,int s) {memset (can,0,sizeof (CAN));d is[s]=0; can[s]=1;queue<int> P;p.push (s); while (!p.empty ( ) {int Now=p.front (); P.pop (); for (int i=point[now];i;i=next[i]) if (Dis[v[i]]>dis[now]+len[i]) {Dis[v[i]]=dis[now  ]+len[i];  if (can[v[i]]==0) {can[v[i]]=1;  P.push (V[i]); }}can[now]=0;}} void Solve () {memset (f,0,sizeof (f)); int head=0,tail=0;for(int i=1;i<=n;i++) if (!ins[i]) q[++tail]=i;while (head<tail) {int now=q[++head];for (int i=point1[now];i;i=next1 [i]) {f[v1[i]]=f[now]+c[i], if (!--Ins[v1[i]]) q[++tail]=v1[i];}} for (int i=1;i<=n;i++) Ans=max (Ans,f[i]);}    int main () {scanf ("%d%d", &n,&m);    scanf ("%d%d%d%d", &s1,&t1,&s2,&t2);     for (int i=1;i<=m;i++) {int x, y, Z, scanf ("%d%d%d", &x,&y,&z);     Insert (x, y, z);    } memset (F1,0x7f,sizeof (F1));    Memset (g1,0x7f,sizeof (G1));    memset (f2,0x7f,sizeof (F2));    Memset (g2,0x7f,sizeof (G2)); SPFA (F1,S1);    LEN1=F1[T1];    SPFA (G1,T1); SPFA (F2,S2);    LEN2=F2[T2];    SPFA (G2,T2); for (int i=1;i<=n;i++) for (int j=point[i];j;j=next[j]) if (f1[i]+len[j]+g1[v[j]]==len1&&f2[i]+len[j]+    G2[V[J]]==LEN2) Add (I,v[j],len[j]), ins[v[j]]++;    Solve ();    memset (f2,0x7f,sizeof (F2));    Memset (g2,0x7f,sizeof (G2)); SPFA (F2,T2);    SPFA (G2,S2); cnt=0;    memset (ins,0,sizeof (INS)); memset (Point1,0,sizeoF (point1));    memset (next1,0,sizeof (NEXT1)); for (int i=1;i<=n;i++) for (int j=point[i];j;j=next[j]) if (f1[i]+len[j]+g1[v[j]]==len1&&f2[i]+len[j]+    G2[V[J]]==LEN2) Add (I,v[j],len[j]), ins[v[j]]++;    Solve (); printf ("%d\n", ans);}



Bzoj 1880: [Sdoi2009]elaxia's route (topology sort +SPFA)

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.