Problem Description


Consider an undirected graph with N vertices, numbered from 1 to N, and M edges. The vertex numbered with 1 corresponds to a mine from where some precious minerals are extracted. The vertex numbered with N corresponds to a minerals processing factory. Each edge has an associated travel time (in time units) and capacity (in units of minerals). It has been decided that the minerals which are extracted from the mine will be delivered to the factory using a single path. This path should have the highest capacity possible, in order to be able to transport simultaneously as many units of minerals as possible. The capacity of a path is equal to the smallest capacity of any of its edges. However, the minerals are very sensitive and, once extracted from the mine, they will start decomposing after T time units, unless they reach the factory within this time interval. Therefore, the total travel time of the chosen path (the sum of the travel times of its edges) should be less or equal to T.


Input


The first line of input contains an integer number X, representing the number of test cases to follow. The first line of each test case contains 3 integer numbers, separated by blanks: N (2 <= N <= 10.000), M (1 <= M <= 50.000) and T (1 <= T <= 500.000). Each of the next M lines will contain four integer numbers each, separated by blanks: A, B, C and D, meaning that there is an edge between vertices A and B, having capacity C (1 <= C <= 2.000.000.000) and the travel time D (1 <= D <= 50.000). A and B are different integers between 1 and N. There will exist at most one edge between any two vertices.


Output


For each of the X test cases, in the order given in the input, print one line containing the highest capacity of a path from the mine to the factory, considering the travel time constraint. There will always exist at least one path between the mine and the factory obbeying the travel time constraint.


Sample Input


2 2 1 10 1 2 13 10 4 4 20 1 2 1000 15 2 4 999 6 1 3 100 15 3 4 99 4


Sample Output


13

99


二分答案最短路验证。

#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include<functional>
using namespace std;
const int maxn = 10005;
int n, m, x, y, z, dis[maxn], T, M, zz, v[5 * maxn];

struct abc
{
int next, v1, v2;
abc(){};
abc(int a, int b, int c) :next(a), v1(b), v2(c){};
};

vector<abc> tree[maxn];

struct cmp
{
bool operator ()(const int &a, const int &b)
{
return dis[a] > dis[b];
}
};

int dijkstra(int begin, int limit)
{
priority_queue<int, vector<int>, cmp> p;
memset(dis, -1, sizeof(dis));
dis[begin] = 0;
p.push(begin);
while (!p.empty())
{
int q = p.top(); p.pop();
for (int i = 0, j; i < tree[q].size(); i++)
{
j = tree[q][i].next;
if (tree[q][i].v1 >= limit)
if (dis[j] == -1 || dis[j]> dis[q] + tree[q][i].v2)
{
dis[j] = dis[q] + tree[q][i].v2;
p.push(j);
}
}
//tree[q].clear();
}
if (dis[n] == -1 || dis[n] > M) return -1;
else return dis[n];
}

int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d%d%d", &n, &m, &M);
for (int i = 1; i <= n; i++) tree[i].clear();
for (int i = 0; i < m; i++)
{
scanf("%d%d%d%d", &x, &y, &z, &zz);
v[i] = z;
tree[x].push_back(abc(y, z, zz));
tree[y].push_back(abc(x, z, zz));
}
sort(v, v + m, greater<int>());
int mid = (m - 1) >> 1;
for (int l = 0, r = m - 1; l < r;)
{
if (dijkstra(1, v[mid]) < 0) l = mid + 1; else r = mid;
mid = (l + r) >> 1;
}
printf("%d\n", v[mid]);
}
return 0;
}