Input
There are multiple test cases.
The first lien of the input contains an integer T (about 100) indicating the number of test cases. Then T cases follow. For each case, the first line contains three integers n, m, k (1 ≤ n ≤ 100, 0 ≤ m ≤ n×(n-1)/2, 0 ≤ k ≤ n, there will be no duplicated friendship) followed by m lines showing the current friendship. The ith friendship contains two integers ui, vi (0 ≤ ui, vi < n, ui ≠ vi) indicating there is friendship between person ui and vi.
Note: The edges in test data are generated randomly.
Output
For each case, print one line containing the answer.
Sample Input
3 4 4 2 0 1 0 2 1 3 2 3 5 5 2 0 1 1 2 2 3 3 4 4 0 5 6 2 0 1 1 2 2 3 3 4 4 0 2 0
Sample Output
2 0 4
#include<iostream> #include<string.h> #include<vector> #include<queue> #include<math.h> #include<stdio.h> #include<algorithm> #define INF 10000000 using namespace std; vector <int > g[102]; int vis[102],d[102],p[102],w[102][102],rank[102];//w[a][b] a到b的 优惠(为 - ) void spfa(int s,int n) { int i; for(i=1;i<=n;i++) d[i]=INF; d[s]=10000; memset(vis,0,sizeof(vis)); queue<int > q; q.push (s); vis[s]=1; while(!q.empty ()) { int a,b; a=q.front (); q.pop (); for(i=0;i<g[a].size ();i++) { b=g[a][i]; if(d[b]>d[a]+w[a][b]) { d[b]=d[a]+w[a][b]; if(!vis[b]) { vis[b]=1; q.push (b); } } } } } int main() { int i,j,m,n,p[102],rank[102],x,a,b; while(scanf("%d%d",&m,&n)!=EOF) { memset(w,0,sizeof(w)); // 一定会有优惠 即w一定会《0 for(i=1;i<=n;i++) { scanf("%d%d%d",&p[i],&rank[i],&x); while(x--) { scanf("%d%d",&a,&b); w[i][a]=b-p[i]; //a i优惠的金币 为- } } for(i=1;i<=n;i++) for(j=1;j<=n;j++) { if(abs(rank[i]-rank[j])<=m&&w[i][j]!=0) { w[i][j]+=p[j]; g[i].push_back (j); } } /*for(i=1;i<=n;i++) { printf("%d ",i); for(j=0;j<g[a].size ();j++) printf("%d ",g[a][j]); printf("\n"); }*/ spfa(1,n); //for(i=1;i<=n;i++) // printf("%d d %d\n",i,d[i]); int minn=INF; for(i=1;i<=n;i++) if(minn>d[i]) minn=d[i]; if(minn<0) minn=0; printf("%d\n",minn); } return 0; }