February 14th every year is the Western Valentine's Day. Valentine's Day is called “qingrenjie” in China, on this day lovers give each other chocolates, cards and roses to express their love or friendship.
Qiu is a boy from Jiangmen, on February 14th he is going to visit a friend Jun, Jun is a girl who loves roses very much. In order to keep up with trends, Qiu want to give Jun a branch of flower as gift. For this special day and special meaning, the number of roses of any branch should be an integer which consist of several 9s, such as 9, 99, 999… (at most nine 9s). After Qiu's research, there are many ways from his home to Jun's home, but there is a limit in every road, that is to say if you take too many roses (more than the limit), you can't go through this load. Furthermore, roses are very expensive (9.9$ each (>_<)), so Qiu did not want to throw any roses away.
In order to please Jun, Qiu ask you a clever acmer to help him, calculate that how many roses he can send most from his house to Jun's house through these roads?
Input
The first line is a number T, then T-case below.
In every case there are two number N,M in the first line.(2 <= N <= 1000, 1 <= M <= 50000) express there has N point in the map,(Qiu is always No.1,Jun is always No.N), and there are M undirected roads in the map.
After that, there are M lines, each line there are 3 numbers (u,v,w),express that there is an undirected road between u and v, and the road's limit is w.(it means if you have x roses, then (x <= w) you can go through this road).
Output
Output how many roses Qiu can send most to Jun.
Sample Input |
Sample Output |
2 3 2 1 2 999 2 3 99 3 3 1 2 999 2 3 99 3 1 9999 |
99 9999
|
Hint
1.Be in sure that there have a way from 1 to N.
2.Have great data entry questions, please use scanf to read data.
#define N 1005 #include<stdio.h> int l[N][N],n; int prim(int n) { int i,j,k,max,mid,sum=0x7fffffff,t[N],low[N]; for(i=1;i<=n;i++) { low[i]=l[1][i]; t[i]=1; } t[1]=0; for(j=2;j<=n;j++) { max=-0x7fffffff; mid=0; for(i=2;i<=n;i++) { if(max<low[i]&&low[i]!=0&&t[i]!=0) { max=low[i]; mid=i; } } t[mid]=0; if(sum>max) { sum=max; } if(mid==n) { return sum; } for(i=2;i<=n;i++) { if(low[i]<l[mid][i]&&l[mid][i]!=0&&t[i]!=0) { low[i]=l[mid][i]; t[i]=mid; } } } } int main() { int i,t,ans,j,k,v1,v2,w,v; scanf("%d",&t); for(i=0;i<t;i++) { scanf("%d %d",&n,&v); for(j=0;j<v;j++) { scanf("%d %d %d",&v1,&v2,&w); l[v1][v2]=w; l[v2][v1]=w; } ans=prim(n); printf("%d/n",ans); for(j=0;j<N;j++) for(k=0;k<N;k++) { l[j][k]=0; } } }