Codeforces 400D (Shortest circuit + and check set) Dima and bacteria

Source: Internet
Author: User



Description

Dima took up the biology of bacteria, as a result of his experiments, he invented k Types of bacteria. Overall, there is N Bacteria at He laboratory right now, and the number of bacteria of type I equals  Ci . For convenience, we'll assume that all the bacteria is numbered from 1To N. The bacteria of type  Ci  is numbered from  To .

With the help of special equipment Dima can move energy from some bacteria to some other one. Of course, the use of such equipment are not free. Dima knows m Ways to move energy from some bacteria to another one. The. I Can is described with integers  ui ,  vi and  xi  Mean that this is the allows moving energy from bacteria with number  ui  To bacteria with number  vi  or vice versa for  xi  Dollars.

Dima's Chef (Inna) calls the Type-distribution   correct   If there is a-a-(may be non-direct)-to-move energy from any bacteria of the particular type to any OT Her bacteria of the same type (between any and bacteria of the same type) for zero cost.

As for correct type-distribution, the cost of moving, the energy depends only on the types of bacteria, help inna to deter Mine is the type-distribution correct? If It is, print the matrix   D   with size   K ? x? K . Cell   D [ i ] J ]   of this matrix must is equal to the minimal possible cost of energy-moving from bacteria with Type  i   to bacteria with type   J .

Input

The first line contains three integers  n,? m,? k (1?≤? ) N. ≤?105; 0?≤? M.≤?105; 1?≤? K. ≤?500) . The next line contains k Integers C1,? C2,?...,? Ck (1?≤? ) Ci? ≤? n) . Each of the next m Lines contains three integers  ui,? Vi,? xi (1?≤? ) Ui,? Vi? ≤?105; 0?≤? Xi? ≤?104) . It's Guaranteed that .

Output

If Dima ' s type-distribution is correct, print string?Yes?, and then k Lines:in the I-th Line Print integers  d[i][1],? D [I] [2],?...,? D [I] [K](d[i] [i]?=?0). If there is no-the-move-from-bacteria I to bacteria J Appropriate  d[i] [J] Must equal to-1. If the type-distribution isn ' t correct print?No?.

Sample Input

Input
4 4 21 32 3 03 4 02 4 12 1 2
Output
Yes0 22 0
Input
3 1 22 11 2 0
Output
Yes0-1-1 0
Input
3 2 22 11 2 02 3 1
Output
Yes0 11 0
Input
3 0 21 2
Output
No



Test instructions: English is really not good, probably describe the first line given N,m,k a set (actually K species of bacteria). n represents n points. (It's actually n bacteria.) ) and 1 to n numbered. The second line gives the number of points in the K collection. (In fact, the number of k species of bacteria). The following M-line, gives the M-bar (in fact, M-type instrument. The u,v,x represents the point of the number U (bacteria), to the point numbered V, which takes X's energy.

The question is whether the energy that satisfies the same bacterial movement is 0. If satisfied, output a matrix of k*k rows, representing the minimum cost required to move between points of different sets (bacteria).

If there is one set that cannot reach another set, output-1.

The title is too abstract ...

A brief description, is to give you n points, M-bar side. K sets, the number of K-sets, and the cost to move between any two points of each set is 0. The cost required to move any two sets.

Idea: Number the points for each set first. Then put it in the picture. Then use and check the set to determine whether to meet the same set of any two points distance of 0. (equivalent to all points of their relationship constituting a set).

If satisfied, the next Floyd the delivery closure, calculate the cost of any two sets of points to move, if not reachable, so that its value is negative one.

Note: The N,m range is large, and the input is __int64 with a long long or.

On the Code

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <cstring> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #define N 100005#define M 505#define maxn 100005#define maxn 200005#define mod 1000000009#defin E INF 0x3f3f3f3f#define Pi ACOs ( -1.0) #define EPS 1e-6typedef Long long ll;using namespace Std;ll n,m,k;ll fa[n];ll num[n];    ll Belong[n];ll Mpt[m][m];ll Find (ll x)//Find {if (x!=fa[x]) Fa[x]=find (fa[x]); return fa[x];}    void Hebing (ll x,ll y)//and check set {x=find (x);    Y=find (y); Fa[y]=x;}    BOOL Judge () {ll i,j,p;    P=1;        for (I=1; i<=k; i++) {int x=find (p);    for (j=1; j<=num[i]; j + +) if (Find (p++)!=x) return 0;    }//Use and check to determine whether any two points in a set will cost 0. return 1;} void Floyd ()//floyd algorithm for transitive closure.  Connect all connected points with the edge and calculate the shortest distance of any two points {for (ll p=1; p<=k; p++) {for (ll i=1; i<=k; i++) {          for (ll J=1; j<=k; J + +) {mpt[i][j]=min (mpt[i][j],mpt[i][p]+mpt[p][j]);    }}}}int Main () {ll i,j,p;        while (scanf ("%lld%lld%lld", &n,&m,&k)!=eof) {memset (mpt,inf,sizeof (MPT)); For (I=1, i<=k; i++) for (j=1; j<=k; J + +) {if (i==j) mpt[i][j]                =mpt[j][i]=0; else Mpt[i][j]=mpt[j][i]=inf;            Initializes the edges of different collections.  } for (I=1; i<=n; i++) fa[i]=i;        And the initialization of the set p=1;  for (I=1; i<=k; i++) {scanf ("%lld", &num[i]);   A set for (j=1; j<=num[i]; j + +) Belong[p++]=i;        Give each point of each set a number} ll u,v,w,uu,vv;            while (m--) {scanf ("%lld%lld%lld", &u,&v,&w);  if (!w) hebing (U,V);            It costs 0 to merge.  Uu=belong[u];   Deposit the number in the picture Vv=belong[v]; Ditto Mpt[uu][Vv]=mpt[vv][uu]=min (MPT[UU][VV],W);  } if (!judge ()) {printf ("no\n");        Judge whether to meet continue;        } printf ("yes\n");        Floyd ();                    For (I=1, i<=k; i++) {for (j=1; j<=k; j + +)//Enumerate all different sets {if (I==J)                mpt[i][j]=0;   if (Mpt[i][j]==inf)//cannot reach mpt[i][j]=-1;                 Make a negative if (j<k) printf ("%lld", Mpt[i][j]);            else printf ("%lld", Mpt[i][j]);        } printf ("\ n"); }} return 0;}


Codeforces 400D (Shortest circuit + and check set) Dima and bacteria

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.