[Usaco2011 Jan] roads and routes

Source: Internet
Author: User

Description
Farmer John is investigating his milk sales plan in a new sales area. He wanted to send the milk to the T-town (1 <= T <= 25,000), numbered 1T. These towns are connected by R-Road (1 <= R <= 50,000, numbered 1 to R) and P-route (1 <= P <= 50,000, numbered 1 to p). Each road I or route I connects Town a_i (1 <= a_i <= t) to B_i (1 <= b_i <= t), which costs c_i. For roads, 0 <= c_i <= 10,000; However, the cost of the route is magical and the cost of c_i may be negative ( -10,000 <= c_i <= 10,000). The road is bidirectional, can be from a_i to B_i, also can from B_i to A_i, spend is c_i. However, the routes are different from a_i to B_i. In fact, because of the recent arrogance of terrorism and the social harmony, there are some policy guarantees: if there is a route from a_i to B_i, it is not possible to return from B_i to a_i through some roads and routes. Since FJ's cow world is recognized for its strength, he needs to transport cows to every town. He wants to find the cheapest solution to send cows to every town from the sending center town S (1 <= s <= T), or know it's impossible.

Input

    • Line 1th: Four spaces separated by integers: T, R, P, and S
    • 2nd to r+1: Three spaces separated by an integer (representing a road): A_i, B_i and C_i
    • Line r+2 to r+p+1: Three spaces separated by an integer (representing a route): A_i, B_i and C_i

Output

    • 1th to T: The minimum cost of reaching town I from S, if no PATH exists.

Sample Input

6 3 3 41 2 53 4 55 6 103 5 -1004 6 -1001 3 -10样例输入解释:一共六个城镇。在1-2,3-4,5-6之间有道路,花费分别是5,5,10。同时有三条航线:3->5,4->6和1->3,花费分别是-100,-100,-10。FJ的中心城镇在城镇4。

Sample Output

NO PATHNO PATH50-95-100样例输出解释:FJ的奶牛从4号城镇开始,可以通过道路到达3号城镇。然后他们会通过航线达到5和6号城镇。但是不可能到达1和2号城镇。

This is the bare single source shortest, right, negative edge directly on the SPFA on the right ...

Then you can get the good results of tle, of course, not rule out-owys optimization

So SPFA has an optimization, SLF optimization, can water over, but I didn't write

Let's talk about how the positive solution is written. Why this problem can not be used Dijkstra, because it has negative edge rights. But we carefully observed that the negative edge can only be a route, and the route will only connect two can not directly to the Unicom block, that is to say, we can run in the Unicom block Dijkstra

What about the link between block and block? This problem after the point is a DAG, then we can topology, with the topological processing blocks and blocks of the relationship between the block directly Dijkstra, then this problem is done

Then there are some detail questions:

    • The topology order needs to start with the S-link block, so there are some changes in the degree of
    • When a unicom block starts to Dijkstra, it needs to throw all the points that are determined by the route into the initial heap.
/*program from wolfycz*/#include <cmath> #include <vector> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define INF 1e9using namespace Std;typedef long Long ll;typedef    unsigned int ui;typedef unsigned long long ull;inline int read () {int X=0,f=1;char ch=getchar (); for (;ch< ' 0 ' | |    Ch> ' 9 '; Ch=getchar ()) if (ch== '-') f=-1;    for (; ch>= ' 0 ' &&ch<= ' 9 '; Ch=getchar ()) x= (x<<1) + (x<<3) +ch-' 0 '; return x*f;}    inline void print (int x) {if (x>=10) print (X/10); Putchar (x%10+ ' 0 ');} const int n=2.5e4,m=5e4;struct s1{int pre[(m<<1) +10],now[n+10],child[(m<<1) +10],val[(M<<1) +10],    Tot    void Join (int x,int y,int z) {pre[++tot]=now[x],now[x]=tot,child[tot]=y,val[tot]=z;} void Insert (int x,int y,int z) {join (x, Y, z), join (Y,X,Z);}}    rod;struct s2{int Pre[m+10],now[n+10],child[m+10],val[m+10],tot; void Join (int x,int y,int z) {pre[++tot]=now[x],now[x]=tot,child[tot]=y,val[tot]=z;}} Pla;struct s3{#define LS (p<<1) #define RS (p<<1|1) #define FA (p>>1) struct node{i        NT X,v;    BOOL operator < (const node &a) Const{return v<a.v;}    }Q[N+10];    int tot;        void Insert (int x,int v) {q[++tot]= (node) {x,v};        int P=tot;    while (P!=1&AMP;&AMP;Q[P]&LT;Q[FA]) swap (Q[P],Q[FA]), P=FA;        } void Delete () {q[1]=q[tot--];        int P=1,son; while (Ls<=tot) {if (rs>tot| |            Q[LS]&LT;Q[RS]) Son=ls;            else Son=rs;            if (Q[son]<q[p]) swap (q[son],q[p]), P=son;        else break; }}}heap;int from[m+10],to[m+10];//Route edge of the beginning and end of the int col[n+10],deg[n+10],dis[n+10];//point belongs to the Unicom block number, the degree of Unicom block, the distance of each point int h[n+10];   BOOL vis[n+10];vector<pair<int,int> >vec[n+10];int n,m,q,s,size;void dfs (int x) {//Flood flood irrigation method if (col[x]==size)    Return    Col[x]=size; for (int p=rod.now[x],son=rod.child[p];p; p=rod.pre[p],son=rod.child[p]) Dfs (son); void Get_deg (int x) {//starting from the current block of S, re-determining the degree if (Vis[x]) return;    Vis[x]=1; for (int p=pla.now[x],son=pla.child[p];p; p=pla.pre[p],son=pla.child[p]) deg[son]++,get_deg (son); void Dijkstra (int type) {for (int i=0;i< (int) vec[type].size (); i++) Heap.insert (vec[type][i].first,vec[type][i].se    COND);        while (heap.tot) {int now=heap.q[1].x;        Heap.delete ();        if (Vis[now]) continue;        Vis[now]=1; for (int p=rod.now[now],son=rod.child[p];p; p=rod.pre[p],son=rod.child[p]) {if (dis[son]>dis[now]+rod.val[p])                {Dis[son]=dis[now]+rod.val[p];            Heap.insert (Son,dis[son]);    }}}}void Topo () {memset (vis,0,sizeof (VIS));    int head=1,tail=1;    H[1]=col[s],vec[col[s]].push_back (Make_pair (s,dis[s]=0));        for (; head<=tail;head++) {int now=h[head];        Dijkstra (now); for (int p=pla.now[now],son=pla.child[p];p; p=pla.pre[p],son=pla.child[p]) {Vec[son].push_back (Make_pair (to[p],d Is[to[p]]=miN (dis[to[p]],dis[from[p]]+pla.val[p]));//Remember to take min if (!) (        --deg[son]) H[++tail]=son;    }}}int Main () {N=read (), M=read (), Q=read (), S=read ();    memset (dis,63,sizeof (dis));        for (int i=1;i<=m;i++) {int x=read (), Y=read (), Z=read ();    Rod.insert (x, y, z);    } for (int i=1;i<=n;i++) if (!col[i]) ++size,dfs (i);        for (int i=1;i<=q;i++) {int x=read (), Y=read (), Z=read ();    Pla.join (col[x],col[y],z), from[pla.tot]=x,to[pla.tot]=y;    } get_deg (Col[s]);    Topo (); for (int i=1;i<=n;i++) printf (dis[i]>inf? ")    NO path\n ":"%d\n ", Dis[i]); return 0;}

[Usaco2011 Jan] roads and routes

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.