#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
using namespace std;
 
int N, M;
vector<pair<intint>> v[20010];
int d[20010];
int Max;
int MaxIdx;
int MaxNum;
 
void dijkstra()
{
    priority_queue<pair<intint>vector<pair<intint>>, greater<pair<intint>>> pq;
    pq.push({01}); // {비용, 정점}
    d[1= 0;
    
    while(!pq.empty())
    {
        int now = pq.top().second;
        int nowCost = pq.top().first;        
        pq.pop();
        
        // 최소비용 아니면 패스 
        if(d[now] != nowCost)
        {
            continue;
        } 
        
        for(int i = 0; i < v[now].size(); i++)
        {
            int next = v[now][i].first;
            int nextCost = v[now][i].second; 
            
            if(d[next] > d[now] + nextCost)
            {
                d[next] = d[now] + nextCost;
                pq.push({d[next], next});
            }
        }
    }
}
 
int main(void)
{
//    freopen("B6118_input.txt", "r", stdin);
    
    cin >> N >> M;
    
    for(int i = 2; i <= N; i++)
    {
        d[i] = 99999999;
    }
    
    for(int i = 1; i <= M; i++)
    {
        int from, to;
        cin >> from >> to;
        
        // 양방향 삽입 
        v[from].push_back({to, 1});
        v[to].push_back({from, 1});    
    }
    
    dijkstra();
 
    for(int i = 2; i <= N; i++)
    {
        if(d[i] > Max)
        {
            Max = d[i];
            MaxIdx = i;
        }
    }
    
    for(int i = 2; i <= N; i++)
    {
        if(Max == d[i])
        {
            MaxNum++;
        }
    }
    
    cout << MaxIdx << " " << Max << " " << MaxNum;
    
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <vector>
#include <map>
#include <queue>
using namespace std;
 
vector<pair<intint>> graph[510];
map<pair<intint>int> not_go;
vector<int> min_track[510];
int d[510];
int V, E, start, arrive;
 
void find_min_track(int start)
{
    priority_queue<pair<intint>vector<pair<intint>>, greater<pair<intint>>> pq;
    pq.push({0, start});
    d[start] = 0;
    
    while(!pq.empty())
    {
        int now = pq.top().second;
        int nowCost = pq.top().first;
        pq.pop();
        
        if(nowCost != d[now])
        {
            continue;    
        }
        
        for(int i = 0; i < graph[now].size(); i++)
        {
            int next = graph[now][i].first;
            int nextCost = graph[now][i].second;
            
            if(nowCost + nextCost < d[next])
            {
                d[next] = nowCost + nextCost;
                pq.push({d[next], next});
                
                min_track[next].clear();
                min_track[next].push_back(now);
            }
            else if(nowCost + nextCost == d[next])
            {
                min_track[next].push_back(now);
            }
        }
    }
}
 
void find_semi_min_track(int start)
{
    priority_queue<pair<intint>vector<pair<intint>>, greater<pair<intint>>> pq;
    pq.push({0, start});
    d[start] = 0;
    
    while(!pq.empty())
    {
        int now = pq.top().second;
        int nowCost = pq.top().first;
        pq.pop();
        
        if(nowCost != d[now])
        {
            continue;    
        }
        
        for(int i = 0; i < graph[now].size(); i++)
        {
            int next = graph[now][i].first;
            int nextCost = graph[now][i].second;
            
            if(not_go[{now, next}] == 1)
            {
                continue;
            }
            
            if(nowCost + nextCost < d[next])
            {
                d[next] = nowCost + nextCost;
                pq.push({d[next], next});
            }
        }
    }
}
 
void del_min_track(int node)
{
    if(node == start)
    {
        return;
    }
    
    for(int i = 0; i < min_track[node].size(); i++)
    {
        not_go[{min_track[node][i], node}] = 1;
        
        del_min_track(min_track[node][i]);
    }
}
 
int main(void)
{
//    freopen("B5719_input.txt", "r", stdin);
    
    while(1)
    {
        cin >> V >> E;
        
        if(V == 0 && E == 0)
        {
            break;
        }
        
        cin >> start >> arrive;
        
        for(int i = 0; i < V; i++)
        {
            d[i] = 199999999;
            graph[i].clear();
            min_track[i].clear();
        }
        not_go.clear();
    
        for(int i = 1; i <= E; i++)
        {
            int from, to, weight;
            cin >> from >> to >> weight;
            
            graph[from].push_back({to, weight});
        }
        
        find_min_track(start);
        del_min_track(arrive);
        
        for(int i = 0; i < V; i++)
        {
            d[i] = 199999999;
        }
        
        find_semi_min_track(start);
        
        if(d[arrive] == 199999999)
        {
            cout << "-1" << "\n";
        }
        else
        {
            cout << d[arrive] << "\n";
        }
    }
    
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
 
#define INF 1000000000000
 
vector<pair<intint>> graph[100010];
long long d_up[100010];
long long d_down[100010];
int height[100010];
int V, E;
int happy, strength;
 
void dijkstra_up(int start)
{
    priority_queue<pair<long longint>vector<pair<long longint>>, greater<pair<long longint>>> pq;
    pq.push({0, start});
    d_up[start] = 0;
    
    while(!pq.empty())
    {
        int now = pq.top().second;
        long long nowCost = pq.top().first;
        pq.pop();
        
        if(nowCost != d_up[now])
        {
            continue;    
        }
        
        for(int i = 0; i < graph[now].size(); i++)
        {
            int next = graph[now][i].first;
            long long nextCost = graph[now][i].second;
            
            if(height[now] < height[next])
            {
                if(nowCost + nextCost < d_up[next])
                {
                    d_up[next] = nowCost + nextCost;
                    pq.push({d_up[next], next});    
                }    
            }    
        }
    }
}
 
void dijkstra_down(int start)
{
    priority_queue<pair<long longint>vector<pair<long longint>>, greater<pair<long longint>>> pq;
    pq.push({0, start});
    d_down[start] = 0;
    
    while(!pq.empty())
    {
        int now = pq.top().second;
        long long nowCost = pq.top().first;
        pq.pop();
        
        if(nowCost != d_down[now])
        {
            continue;    
        }
        
        for(int i = 0; i < graph[now].size(); i++)
        {
            int next = graph[now][i].first;
            long long nextCost = graph[now][i].second;
            
            if(height[now] < height[next])
            {
                if(nowCost + nextCost < d_down[next])
                {
                    d_down[next] = nowCost + nextCost;
                    pq.push({d_down[next], next});
                }    
            }    
        }
    }
}
 
int main(void)
{
//    freopen("B16681_input.txt", "r", stdin);
    
    cin >> V >> E >> strength >> happy;
    
    for(int i = 1; i <= V; i++)
    {
        cin >> height[i];
        
        d_up[i] = INF;
        d_down[i] = INF;
    }
 
    for(int i = 1; i <= E; i++)
    {
        int from, to, weight;
        cin >> from >> to >> weight;
        
        graph[from].push_back({to, weight});
        graph[to].push_back({from, weight});
    }
    
    dijkstra_up(1);
    dijkstra_down(V);
    
    long long Max = -INF;
    for(int i = 2; i < V; i++)
    {
        if(Max < happy*height[i] - strength*d_up[i] - strength*d_down[i])
        {
            Max = happy*height[i] - strength*d_up[i] - strength*d_down[i];
        }
    }
    
    if(Max == -INF)
    {
        cout << "Impossible";
    }
    else
    {
        cout << Max;    
    }
    
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
 
vector<pair<intint>> graph[1010];
vector<int> track[1010];
int d[1010];
int V, E;
int lineCnt;
 
void dijkstra(int start)
{
    priority_queue<pair<intint>vector<pair<intint>>, greater<pair<intint>>> pq;
    pq.push({0, start});
    d[start] = 0;
    
    while(!pq.empty())
    {
        int now = pq.top().second;
        int nowCost = pq.top().first;
        pq.pop();
        
        if(nowCost != d[now])
        {
            continue;    
        }
        
        for(int i = 0; i < graph[now].size(); i++)
        {
            int next = graph[now][i].first;
            int nextCost = graph[now][i].second;
            
            if(nowCost+nextCost < d[next])
            {
                d[next] = nowCost+nextCost;
                pq.push({d[next], next});
                
                track[next].clear();
                track[next].push_back(now);
            }
        }
    }
}
 
int main(void)
{
//    freopen("B2211_input.txt", "r", stdin);
    
    cin >> V >> E;
    
    for(int i = 1; i <= V; i++)
    {
        d[i] = 199999999;
    }
 
    for(int i = 1; i <= E; i++)
    {
        int from, to, weight;
        cin >> from >> to >> weight;
        
        graph[from].push_back({to, weight});
        graph[to].push_back({from, weight});
    }
    
    dijkstra(1);
    
    for(int i = 1; i <= V; i++)
    {
        lineCnt += track[i].size();
    }
    
    cout << lineCnt;
    
    for(int i = 1; i <= V; i++)
    {
        for(int j = 0; j < track[i].size(); j++)
        {
            cout << i << " "  << track[i][j];
        }
        cout << "\n";
    }
    
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
 
int graph[1010][1010];
int V, E, arrive;
int Max;
 
void floyd()
{
    for(int k = 1; k <= V; k++)
    {
        for(int i = 1; i <= V; i++)
        {
            for(int j = 1; j <= V; j++)
            {
                if(graph[i][k] + graph[k][j] < graph[i][j])
                {
                    graph[i][j] = graph[i][k] + graph[k][j];
                }
            }
        }
    }
}
 
int main(void)
{
//    freopen("B1238_input.txt", "r", stdin);
    
    cin >> V >> E >> arrive;
 
    for(int i = 1; i <= V; i++)
    {
        for(int j = 1; j <= V; j++)
        {
            if(i == j)
            {
                graph[i][j] = 0;
            }
            else
            {
                graph[i][j] = 99999999;            
            }
        }
    } 
    
    for(int i = 1; i <= E; i++)
    {
        int from, to, weight;
        cin >> from >> to >> weight;
        
        graph[from][to] = weight;
    }
    
    floyd();
    
    for(int i = 1; i <= V; i++)
    {
        if(Max < graph[i][arrive] + graph[arrive][i])
        {
            Max = graph[i][arrive] + graph[arrive][i];
        }
    }
    
    cout << Max;
    
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
 
int graph[1010][1010];
int V, E, arrive;
int Max;
 
void floyd()
{
    for(int k = 1; k <= V; k++)
    {
        for(int i = 1; i <= V; i++)
        {
            for(int j = 1; j <= V; j++)
            {
                if(graph[i][k] + graph[k][j] < graph[i][j])
                {
                    graph[i][j] = graph[i][k] + graph[k][j];
                }
            }
        }
    }
}
 
int main(void)
{
//    freopen("B1238_input.txt", "r", stdin);
    
    cin >> V >> E >> arrive;
 
    for(int i = 1; i <= V; i++)
    {
        for(int j = 1; j <= V; j++)
        {
            if(i == j)
            {
                graph[i][j] = 0;
            }
            else
            {
                graph[i][j] = 99999999;            
            }
        }
    } 
    
    for(int i = 1; i <= E; i++)
    {
        int from, to, weight;
        cin >> from >> to >> weight;
        
        graph[from][to] = weight;
    }
    
    floyd();
    
    for(int i = 1; i <= V; i++)
    {
        if(Max < graph[i][arrive] + graph[arrive][i])
        {
            Max = graph[i][arrive] + graph[arrive][i];
        }
    }
    
    cout << Max;
    
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
 
vector<pair<intint>> graph[1010];
int d[1010];
int V, E, start, arrive;
 
void dijkstra(int start)
{
    priority_queue<pair<intint>vector<pair<intint>>, greater<pair<intint>>> pq;
    pq.push({0, start});
    d[start] = 0;
    
    while(!pq.empty())
    {
        int now = pq.top().second;
        int nowCost = pq.top().first;
        pq.pop();
        
        if(nowCost != d[now])
        {
            continue;    
        }
        
        for(int i = 0; i < graph[now].size(); i++)
        {
            int next = graph[now][i].first;
            int nextCost = graph[now][i].second;
            
            if(nowCost+nextCost < d[next])
            {
                d[next] = nowCost+nextCost;
                pq.push({d[next], next});
            }
        }
    }
}
 
int main(void)
{
//    freopen("B1916_input.txt", "r", stdin);
    
    cin >> V >> E;
    
    for(int i = 1; i <= V; i++)
    {
        d[i] = 199999999;
    }
 
    for(int i = 1; i <= E; i++)
    {
        int from, to, weight;
        cin >> from >> to >> weight;
        
        graph[from].push_back({to, weight});
    }
    
    cin >> start >> arrive;
    
    dijkstra(start);
    
    cout << d[arrive];
    
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
 
vector<pair<intint>> graph[20010];
int d[20010];
int V, E, start;
 
void dijkstra(int start)
{
    priority_queue<pair<intint>vector<pair<intint>>, greater<pair<intint>>> pq;
    pq.push({0, start});
    d[start] = 0;
    
    while(!pq.empty())
    {
        int now = pq.top().second;
        int nowCost = pq.top().first;
        pq.pop();
        
        if(nowCost != d[now])
        {
            continue;    
        }
        
        for(int i = 0; i < graph[now].size(); i++)
        {
            int next = graph[now][i].first;
            int nextCost = graph[now][i].second;
            
            if(nowCost+nextCost < d[next])
            {
                d[next] = nowCost+nextCost;
                pq.push({d[next], next});
            }
        }
    }
}
 
int main(void)
{
//    freopen("B1753_input.txt", "r", stdin);
    
    cin >> V >> E >> start;
    
    for(int i = 1; i <= V; i++)
    {
        d[i] = 9999999;
    }
 
    for(int i = 1; i <= E; i++)
    {
        int from, to, weight;
        cin >> from >> to >> weight;
        
        graph[from].push_back({to, weight});
    }
    
    dijkstra(start);
    
    for(int i = 1; i <= V; i++)
    {
        if(d[i] == 9999999)
        {
            cout << "INF" << "\n";    
        }
        else
        {
            cout << d[i] << "\n";        
        }
    }
    
    return 0;
}
cs

+ Recent posts