输入输出样例
输入 #1复制
5 4 1 5 4 1 2 2 2 5 2 4 1 1
输出 #1复制
4 2
这题目真tm坑 卡前向星判重 有两个点死活tle 可能蒟蒻的我太菜了 只能用邻接矩阵来写
#include<bits/stdc++.h>
const int maxn = 2e3 + 10;
int n, m;
int tot;
int u = 0;
//int head[maxn];
int vis[maxn];
int dis[maxn];
int cnt[maxn];
int mp[maxn][maxn];
using namespace std;
/*struct node{
int to;
int next;
int w;
node() {}
node(int a, int b, int c) : to(a), next(b), w(c) {}
}edge[maxn * maxn];*/
void init(){
tot = 0;
//memset(head, -1, sizeof(head));
memset(mp, 999999, sizeof(mp));
}
void init1(){
memset(vis, 0, sizeof(vis));
memset(dis, 999999, sizeof(dis));
memset(cnt, 0, sizeof(cnt));
}
void spfa(){
init1();
queue<int> q;
q.push(1);
vis[1] = 1;
dis[1] = 0;
cnt[1] = 1;
while(!q.empty()){
int nw = q.front();
q.pop();
vis[nw] = 0;
if(nw == n)
continue;
for(int i = 1; i <= n; i++){
if(dis[i] == dis[nw] + mp[nw][i]){
cnt[i] += cnt[nw];
}
if(dis[i] > dis[nw] + mp[nw][i]){
dis[i] = dis[nw] + mp[nw][i];
cnt[i] = cnt[nw];
}
if(!vis[i] && cnt[i]){
q.push(i);
vis[i] = 1;
}
}
cnt[nw] = 0;
}
if(dis[n] >= 999999)
cout << "No answer" << endl;
else{
cout << dis[n] << " " << cnt[n] << endl;
}
}
/*void edgeadd(int a, int b, int c){
edge[tot] = node(b, head[a], c);
head[a] = tot++;
}*/
int main(){
cin >> n >> m;
init();
for(int i = 1; i <= m; i++){
int a, b, c;
cin >> a >> b >> c;
if(mp[a][b] == 0)
mp[a][b] = c;
else
mp[a][b] = min(mp[a][b], c);
}
spfa();
return 0;
}