开始用dfs来做,发现最后一个测试点怎么也过不去,只能用dij来做了
#include<iostream>
#include<vector>
#include<climits>
#pragma warning(disable:4996)
using namespace std;
struct ArcNode {
int length;
int time;
ArcNode() :length(0), time(0) {}
};
int s_beg, s_end;
vector<vector<ArcNode>> arc;
struct node {
vector<int> P;
int time;
int length;
node() { time = INT_MAX;length = INT_MAX; }
};
vector<bool> visited;
node tim, dis;
//node tim, dis,temp;
int N, M;
//void dfs(int index)
//{
// if (index == s_end)
// {
// if (temp.length < dis.length ||
// (temp.length == dis.length && temp.time < dis.time))
// dis = temp;
// if (temp.time < tim.time ||
// (temp.time == tim.time && temp.P.size() < tim.P.size()))
// tim = temp;
// return;
// }
// if (temp.length >= dis.length && temp.time >= tim.time) return;
//
// for (int t = 0;t < N;t++)
// {
// if (arc[index][t].length && !visited[t] )
// {
// visited[t] = true;
// temp.P.push_back(t);
// temp.length += arc[index][t].length;
// temp.time += arc[index][t].time;
// dfs(t);
// visited[t] = false;
// temp.P.pop_back();
// temp.length -= arc[index][t].length;
// temp.time -= arc[index][t].time;
// }
// }
//}
vector<node> temp;
vector<node> re1;
void dij1()
{
temp.resize(N);
re1.resize(N);
visited[s_beg] = true;
re1[s_beg].length = re1[s_beg].time = 0;
re1[s_beg].P.push_back(s_beg);
for (int t = 0;t < N;t++)
if (!visited[t] && arc[s_beg][t].length != 0) {
temp[t].length = arc[s_beg][t].length;
temp[t].time = arc[s_beg][t].time;
temp[t].P.push_back(s_beg);
}
while (!visited[s_end])
{
node m;int v;
for (int t = 0;t < N;t++)
if (!visited[t] && temp[t].length < m.length)
{
m = temp[t];
v = t;
}
m.P.push_back(v);
re1[v] = m;
visited[v] = true;
for (int t = 0;t < N;t++)
if (!visited[t] && arc[v][t].length != 0)
{
if (m.length + arc[v][t].length < temp[t].length ||
(m.length + arc[v][t].length == temp[t].length && m.time + arc[v][t].time < temp[t].time))
{
temp[t] = m;temp[t].length += arc[v][t].length;temp[t].time += arc[v][t].time;
}
}
}
}
void dij2()
{
temp.resize(N);
re1.resize(N);
visited[s_beg] = true;
re1[s_beg].length = re1[s_beg].time = 0;
re1[s_beg].P.push_back(s_beg);
for (int t = 0;t < N;t++)
if (!visited[t] && arc[s_beg][t].time != 0) {
temp[t].time = arc[s_beg][t].time;
temp[t].P.push_back(s_beg);
}
while (!visited[s_end])
{
node m;int v;
for (int t = 0;t < N;t++)
if (!visited[t] && temp[t].time < m.time)
{
m = temp[t];
v = t;
}
m.P.push_back(v);
re1[v] = m;
visited[v] = true;
for (int t = 0;t < N;t++)
if (!visited[t] && arc[v][t].time != 0)
{
if (m.time + arc[v][t].time < temp[t].time ||
(m.time + arc[v][t].time == temp[t].time &&m.P.size()<temp[t].P.size()))
{
temp[t] = m;temp[t].time += arc[v][t].time;
}
}
}
}
int main()
{
scanf("%d %d", &N, &M);
arc.resize(N, vector<ArcNode>(N));
visited.assign(N, false);
while (M--)
{
int a, b, c, d, e;
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
if (c == 1)
{
arc[a][b].length = d;arc[a][b].time = e;
}
else
{
arc[a][b].length = arc[b][a].length = d;
arc[a][b].time = arc[b][a].time = e;
}
}
scanf("%d %d", &s_beg, &s_end);
/*visited[s_beg] = true;
temp.P.push_back(s_beg);
temp.length = temp.time = 0;*/
/*dfs(s_beg);*/
dij1();
dis = re1[s_end];
visited.assign(N, false);
temp.clear();
re1.clear();
dij2();
tim = re1[s_end];
if (dis.P == tim.P)
{
printf("Distance = %d; Time = %d: %d", dis.length, dis.time,dis.P[0]);
for (unsigned int t = 1;t < dis.P.size();t++)
printf(" -> %d", dis.P[t]);
putchar('\n');
}
else
{
printf("Distance = %d: %d", dis.length, dis.P[0]);
for (unsigned int t = 1;t < dis.P.size();t++)
printf(" -> %d", dis.P[t]);
putchar('\n');
printf("Time = %d: %d", tim.time, tim.P[0]);
for (unsigned int t = 1;t < tim.P.size();t++)
printf(" -> %d", tim.P[t]);
putchar('\n');
}
}