标签:using top ios amp mem iostream class syn ack
给定一个下三角矩阵,询问从1开始到其他点的最短路径中,最长的那个是多少?(其中x代表没路径)
水题,dijkstra即可AC,唯一不同的是多一个字符串处理
#include<vector>
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<set>
#include<cstring>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<int,pii> PII;
const int maxn = 1e3+5;
struct Edge
{
int to,cost;
};
vector<Edge> G[maxn];
int dis[maxn];
bool vis[maxn];
int N;
void dijkstra()
{
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
priority_queue<pii,vector<pii>,greater<pii> > Q;
Q.push(pii(0,1));
dis[1] = 0;
while(!Q.empty()){
pii t = Q.top();
Q.pop();
int u = t.second;
int d = t.first;
if(vis[u])continue;
vis[u] = 1;
for(int i=0;i<G[u].size();i++){
Edge e = G[u][i];
if(e.cost + d < dis[e.to]){
dis[e.to] = e.cost + d;
Q.push(pii(dis[e.to],e.to));
}
}
}
int ans = 0;
for(int i=2;i<=N;i++){
ans = max(ans,dis[i]);
}
cout<< ans << endl;
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
int t;
cin.get();
string s;
for(int j=2;j<=N;j++){
getline(cin,s);
int k = 1;
int len = s.size();
for(int i=0;i<len;i++){
int num = 0;
if(s[i]==‘ ‘)continue;
if(s[i]==‘x‘){
k++;
continue;
}
while(s[i]>=‘0‘&&s[i]<=‘9‘){
num *= 10;
num += (s[i]-‘0‘);
i++;
}
G[k].push_back((Edge){j,num});
G[j].push_back((Edge){k,num});
k++;
}
}
dijkstra();
return 0;
}
标签:using top ios amp mem iostream class syn ack
原文地址:https://www.cnblogs.com/django-lf/p/9728865.html