People alive series of Fang elder sister and Fang elder sister's Pig Time limit:1000ms Memory limit:65536k Topic Description Narration
Fang elder sister particularly like Pig, so, she deliberately raised m a pigsty, by the way in the K -bar without the edge, each side has a beginning v, distance ... . Fang elder sister and the pig agreed well. Go to a fixed pigsty every day to eat. Fang elder sister in order not to tired her cute pigs, want to know all the pig eat to go the shortest distance is how much?
Enter first line,Number of Pigsm(K (1<=k<=1200).(Pig's number is1..m)
n+1YesNPig's pigsty in the first place,line n+k+1 :u,1<=w<=255)
m pigsty connected.
Output Demo Sample input
3 4 52341 2 11 3 52 3 72 4 33 4 5
Demo sample Output
8
Tips
Beginning to think, Cui teacher pit us, not the shortest way, hit the smallest spanning tree. Kurskal
The feeling is right, there is no pigsty. Don't add sides. ╮(╯▽╰)╭, Hung 20 times. Not ever.
Error code:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib>const int INF = 65535; using namespace std;struct node{int u,v,w;} Edge[1300];int n,m,father[610],l;bool vis[610];int cmp (const void *a, const void *b) {struct node *x,*y; X = (struct node *) A; Y = (struct node *) b; return x->w-y->w;} void Init () {L = 0; for (int i = 0;i<=m;i++) father[i] = i;} int findx (int r) {int I, J; while (R!=father[r]) {r= father[r]; } i = r; while (father[i]!=r) {j = father[i]; Father[i] = r; i = j; } return R;} void Kruskal () {int ans = 0; for (int i = 0;i<=l;i++) {int uu = FINDX (EDGE[I].U); int vv = FINDX (EDGE[I].V); printf ("%d->%d =%d\n", EDGE[I].U,EDGE[I].V,EDGE[I].W); if (UU!=VV) {Father[uu] = VV; Ans + = EDGE[I].W; }} printf ("%d\n", ans);} int main () {int mm, a, b,cK scanf ("%d%d%d", &n,&m,&k); Init (); for (int i = 1;i<=m;i++) vis[i] = 1; for (int i = 0;i<n;i++) {scanf ("%d", &mm); VIS[MM] = 0; } for (int i = 0;i<k;i++) {scanf ("%d%d%d", &a,&b,&c); if (vis[a]==1 | | vis[b]==1) continue; EDGE[L].U = A; EDGE[L].V = b; EDGE[L++].W = C; } qsort (Edge,l,sizeof (edge[0]), CMP); Kruskal (); return 0;}
AC
Background data Compare to water
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #define INF 0x3f3f3f3fusing namespace Std;int n,m,kkk;int ma[651][651],a[400];void init () {int i,j; For (I=1, i<=m;i++) {for (j=1; j<=m; J + +) {if (i==j) ma[i][j] = 0;else ma[i][j]=inf;} }}int Main () {int u,v,w,i,j,k;scanf ("%d%d%d", &N,&M,&KKK);//puts ("1"); init (); for (I=1; i<=n; i++) {scanf ("%d", &a[i]);} for (i = 0;i<kkk;i++) {scanf ("%d%d%d", &u,&v,&w); Ma[u][v]=ma[v][u]=w; } for (k=1, k<=m; k++) {for (I=1, i<=m; i++) {for (j=1; j<=m; J + +) {if (Ma[i][j] > Ma[i] [K] + ma[k][j]) {ma[i][j] = Ma[i][k] + ma[k][j];} }}} int sum, MI = INF; for (I=1; i<=m; i++) {sum=0; for (j=1; j<=n; J + +) {sum + = Ma[i][a[j]];} if (Sum < mi) mi = sum; } printf ("%d\n", MI); return 0;} /*****************************
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
People Alive series Tanya and Cai Jian Ya Pig (Floyd)