题目描述
Professional Ability Test (PAT) consists of several series of subject tests. Each test is divided into several levels. Level A is a prerequisite (前置要求) of Level B if one must pass Level A with a score no less than S in order to be qualified to take Level B. At the mean time, one who passes Level A with a score no less than S will receive a voucher(代金券)of D yuans (Chinese dollar) for taking Level B.
At the moment, this PAT is only in design and hence people would make up different plans. A plan is NOT consistent if there exists some test T so that T is a prerequisite of itself. Your job is to test each plan and tell if it is a consistent one, and at the mean time, find the easiest way (with minimum total S) to obtain the certificate of any subject test. If the easiest way is not unique, find the one that one can win the maximum total value of vouchers.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (≤1000) and M, being the total numbers of tests and prerequisite relations, respectively. Then M lines follow, each describes a prerequisite relation in the following format:
T1 T2 S D
where T1 and T2 are the indices (from 0 to N−1) of the two distinct tests; S is the minimum score (in the range (0, 100]) required to pass T1 in order to be qualified to take T2; and D is the value of the voucher (in the range (0, 500]) one can receive if one passes T1 with a score no less than S and plan to take T2. It is guaranteed that at most one pair of S and D are defined for a prerequisite relation.
Then another positive integer K (≤N) is given, followed by K queries of tests. All the numbers in a line are separated by spaces.
Output Specification:
Print in the first line Okay. if the whole plan is consistent, or Impossible. if not.
If the plan is consistent, for each query of test T, print in a line the easiest way to obtain the certificate of this test, in the format:
T0->T1->…->T
However, if T is the first level of some subject test (with no prerequisite), print You may take test T directly. instead.
If the plan is impossible, for each query of test T, check if one can take it directly or not. If the answer is yes, print in a line You may take test T directly.; or print Error. instead.
样例
blablabla
最短路
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<unordered_map>
#include<queue>
#include<vector>
#include<unordered_set>
using namespace std;
const int N = 1e3 + 10;
int n, m, q;
int g[N][N], gain[N][N], dis[N], money[N], pre[N];//图的边权,得到的,花费,得到的钱,前驱
bool st[N], haspre[N];//st 已经在? 有前驱?
string print[N];
unordered_set<int> vertex;
bool djkstra(int T)
{
memset(st, false, sizeof st);
memset(dis, 0x3f, sizeof dis);
memset(money, 0, sizeof money);
memset(pre, 0, sizeof pre);
for (int i = 0; i < n; i++) if (!haspre[i]) dis[i] = 0;
for (int i = 0; i < vertex.size(); i++)
{
int t = -1;
for (auto j : vertex)
{
if (!st[j] && (t == -1 || dis[j] < dis[t])) t = j;
}
st[t] = true;
for (auto j : vertex)
{
if (j == 4)
{
int ppp = j + 1;
}
//renew
if (dis[t] + g[t][j] < dis[j])
{
dis[j] = dis[t] + g[t][j];
pre[j] = t;
money[j] = money[t] + gain[t][j];
}
else if (dis[t] + g[t][j] == dis[j])
{
if (money[t] + gain[t][j] > money[j])
{
pre[j] = t;
money[j] = money[t] + gain[t][j];
}
}
}
}
if (dis[T] == 0x3f3f3f3f) return false;
else
{
string res = "";
vector<int> num;
int i = T;
for (; haspre[i]; i = pre[i]) num.push_back(i);
num.push_back(i);
reverse(num.begin(), num.end());
for (int i = 0; i < num.size(); i++)
{
res += to_string(num[i]);
if (i != num.size() - 1) res += "->";
}
print[T] = res;
return true;
}
}
int main()
{
cin >> n >> m;
memset(g, 0x3f, sizeof g);
memset(dis, 0x3f, sizeof dis);
int a, b, c, d;
for (int i = 0; i < m; i++)
{
scanf("%d %d %d %d", &a, &b, &c, &d);
haspre[b] = true;
if (g[a][b] > c) g[a][b] = c, gain[a][b] = d;
else if (g[a][b] == c) gain[a][b] = min(gain[a][b], d);
}
cin >> q;
int x;
bool flag = true;
vector<string> output;
for (int i = 0; i < q; i++)
{
scanf("%d", &x);
vertex.insert(x);
if (!haspre[x])
{
flag = true;
string temp = "You may take test " + to_string(x) + " directly.";
output.push_back(temp);
}
else if (djkstra(x))
{
output.push_back(print[x]);
flag = true;
}
else
{
flag = false;
output.push_back("Error.");
}
}
if (flag) cout << "Okay." << endl;
else cout << "Impossible." << endl;
for (int i = 0; i < output.size(); i++) cout << output[i] << endl;
return 0;
}