結果
| 問題 |
No.17 2つの地点に泊まりたい
|
| コンテスト | |
| ユーザー |
myanta
|
| 提出日時 | 2017-05-01 14:00:35 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 16 ms / 5,000 ms |
| コード長 | 1,516 bytes |
| コンパイル時間 | 489 ms |
| コンパイル使用メモリ | 55,680 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-15 01:40:21 |
| 合計ジャッジ時間 | 1,338 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:110:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
110 | scanf("%d", &point[i].s);
| ~~~~~^~~~~~~~~~~~~~~~~~~
main.cpp:112:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
112 | scanf("%d", &m);
| ~~~~~^~~~~~~~~~
main.cpp:115:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
115 | scanf("%d%d%d", &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
struct road_t
{
int to, c;
};
struct point_t
{
int s;
vector<road_t> road;
};
struct state_t
{
int c, p, s;
};
struct state_cmp
{
bool operator () (const state_t& lhs, const state_t& rhs) const
{
return lhs.c>rhs.c;
}
};
priority_queue<state_t, vector<state_t>, state_cmp> que;
int min_u(int& m, int v)
{
if(m>v)
{
m=v;
return 1;
}
return 0;
}
void push(int c, int p, int s)
{
que.push((state_t){c, p, s});
}
int pop(int& c, int& p, int& s)
{
if(que.empty()) return 0;
state_t w=que.top();
c=w.c;
p=w.p;
s=w.s;
que.pop();
return 1;
}
int solve(vector<point_t>& point)
{
int c=0, p=0, s=0, n=point.size();
vector<int> dp(n*n, 50*1000*2);
push(c, p, s);
for(;pop(c, p, s);)
{
int idx=s*n+p;
if(min_u(dp[idx], c)==0) continue;
for(auto r: point[p].road)
{
push(c+r.c, r.to, s);
}
if(p==0 || p==n-1 || s==n-1 || s==p) continue;
int ns=p;
if(s>0) ns=n-1;
push(c+point[p].s, p, ns);
}
return dp.back();
}
int main(void)
{
int n, m, i, a, b, c;
vector<point_t> point;
while(scanf("%d", &n)==1)
{
point.clear();
point.resize(n);
for(i=0;i<n;i++)
{
scanf("%d", &point[i].s);
}
scanf("%d", &m);
for(i=0;i<m;i++)
{
scanf("%d%d%d", &a, &b, &c);
point[a].road.push_back((road_t){b, c});
point[b].road.push_back((road_t){a, c});
}
printf("%d\n", solve(point));
}
return 0;
}
myanta