Failing Components

As a jury member of the Best Architectural Planning Contest, you are tasked with scoring the reliability of a system. All systems entered in the contest consist of a number of components which depend on each other. The reliability of such a system depends on the damage done by a failing component. Ideally a failing component should have no consequences, but since most components depend on each other, some other components will usually fail as well.

Most components are somewhat resilient to short failures of the components they depend on. For example, a database could be unavailable for a minute before the caches expire and new data must be retrieved from the database. In this case, the caches can survive for a minute after a database failure, before failing themselves. If a component depends on multiple other components which fail, it will fail as soon as it can no longer survive the failure of at least one of the components it depends on. Furthermore no component depends on itself directly, however indirect self-dependency through other components is possible.

You want to know how many components will fail when a certain component fails, and how much time passes before all components that will eventually fail, actually fail. This is difficult to calculate by hand, so you decided to write a program to help you. Given the description of the system, and the initial component that fails, the program should report how many components will fail in total, and how much time passes before all those components have actually failed.

Input Format

On the first line one positive number: the number of test cases, at most 100. After that per test case:

  • one line with three space-separated integers nndd and cc (1leq n leq 100001n10000 and 1 leq d leq 1000001d100000 and 1 leq c leq n1cn): the total number of components in the system, the number of dependencies between components, and the initial component that fails, respectively.

  • dd lines with three space-separated integers aabb and ss (1 leq a,b leq n1a,bn and a != ba !=b and 0 leq s leq 1 0000s1000), indicating that component aa depends on component bb, and can survive for ss seconds when component bbfails.

In each test case, all dependencies (a, b)(a,b) are unique.

Output Format

Per test case:

  • one line with two space-separated integers: the total number of components that will fail, and the number of seconds before all components that will fail, have actually failed.

样例输入

2
3 2 2
2 1 5
3 2 5
3 3 1
2 1 2
3 1 8
3 2 4

样例输出

2 5
3 6

迪杰斯特拉

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+3, inf = 1e9+7;
int u[N],v[N],w[N],first[N],nxt[N],dis[N];
bool book[N];
int t,n,d,c;


void init()
{
    memset(first,-1, sizeof(first));
    memset(book,0, sizeof(book));
    for(int i = 1; i <= n; ++i){
        dis[i] = inf;
    }
}




void solve()
{
    for(int i = 1; i <= n-1; ++i){
        int min = inf,u;
        for(int j = 1; j <= n; ++j){
            if(book[j]==0&&dis[j]<min){
                min = dis[j];
                u = j;
            }
        }
        book[u] = 1;
        for(int l = first[u]; l != -1; l = nxt[l]){
            if(w[l]<inf){
                if(dis[u] + w[l] < dis[v[l]])//松弛边
                    dis[v[l]] = dis[u]+w[l];
            }
        }
    }
    //找出单源最短路中的最长路径
   int num=0;
int maxx=-1;
    for(int i=1;i<=n;i++){
    if(dis[i]!=inf){
    num++;
    maxx=max(maxx,dis[i]);
}
}
printf("%d %d
",num,maxx);
}


int main()
{
    //freopen("in.txt","r",stdin);
    cin>>t;
    while(t--)
    {
        scanf("%d%d%d",&n,&d,&c);
        init();
        for(int i = 1; i <= d; ++i){
            scanf("%d%d%d",&v[i],&u[i],&w[i]);
           //链式前项星存图
            nxt[i] = first[u[i]];
            first[u[i]] = i;
            if(u[i]==c)
                dis[v[i]] = w[i];
        }
        book[c] = 1;
        dis[c] = 0;
        solve();
    }


    return 0;
}
原文地址:https://www.cnblogs.com/kuroko-ghh/p/9363344.html