zoukankan      html  css  js  c++  java
  • poj 3268 Silver Cow Party(dijkstra ++ 双向最短路)

    最近做图论的题很是心塞,在做这道题的时候去百度了大神的博客,发现大神真心的任性,理解的透彻,dijkstra可以随便改,这篇博客在参考大神后,在自己的理解上,加以改动,顺便附上详细的解释,,,

    One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cowparty to be held at farm #X (1 ≤ X  N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

    Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.

    Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

    Input

    Line 1: Three space-separated integers, respectively: NM, and X 
    Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: Ai,Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.

    Output

    Line 1: One integer: the maximum of time any one cow must walk.

    Sample Input

    4 8 2
    1 2 4
    1 3 2
    1 4 7
    2 1 1
    2 3 5
    3 1 2
    3 4 4
    4 2 3

    Sample Output

    10

    Hint

    Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.

            AC:代码

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn = (int)1e3+5;
    #define INF 0x3f3f3f3f
    
    int n,m,st;
    int map[maxn][maxn];//储存有向图 
    int dis1[maxn],dis2[maxn];//正向di距离  ,反向di距离 
    int vis1[maxn],vis2[maxn];//同理正向标记 ,反向标记 
    
    void dijkstra(int s)
    {
    	int pos1,pos2,v1,v2,minn1,minn2;
    	for(int i = 1;i<=n;i++)
    	{
    		vis1[i] = vis2[i] = 0;
    		dis1[i] = dis2[i] = INF;
    	}
    	vis1[s] = vis2[s] = 1;
    	dis1[s] = dis2[s] = 0;
    	pos1 = s,pos2 = s;
    	for(int i = 1;i<=n-1;i++)//核心代码 
    	{
    		for(int j = 1;j<=n;j++)
    		{//注意两个if中的pos位置不一样 ,此处是核心 
    			if(!vis1[j] && map[pos1][j]!=INF && dis1[j]>dis1[pos1] + map[pos1][j])
    			{
    				dis1[j] = dis1[pos1] + map[pos1][j];
    			}
    			if(!vis2[j] && map[j][pos2]!=INF && dis2[j] > dis2[pos2] + map[j][pos2]) 
    			{
    				dis2[j] = dis2[pos2] + map[j][pos2];
    			}
    		}
    		minn1 = minn2 = INF;
    		for(int j = 1;j<=n;j++)
    		{//正常的di思路,不多做介绍 
    			if(!vis1[j] && minn1 >dis1[j])
    			{
    				v1 = j;
    				minn1 = dis1[j];
    			}
    			if(!vis2[j] && minn2 > dis2[j])
    			{
    				v2 = j;
    				minn2 = dis2[j];
    			}
    		}
    		vis1[v1] = 1;//标记 
    		vis2[v2] = 1;
    		pos1 = v1;//pos不要忘记重赋值 
    		pos2 = v2;
    	}
    }
    int main()
    {
    	cin>>n>>m>>st;
    	int a,b,c;
    	memset(map,INF,sizeof(map));
    	for(int i = 1;i<=m;i++)
    	{
    		scanf("%d %d %d",&a,&b,&c);
    		map[a][b] = c;//有向图赋初值 
    	}
    	dijkstra(st);//从终点开始搜索 
    	int ans = 0;
    	for(int i = 1;i<=n;i++)
    	{
    		if(i==st) continue;
    		if(dis1[i] == INF || dis2[i] == INF ) continue;
    		if(dis1[i] + dis2[i] > ans) ans = dis1[i] + dis2[i];
    	}
    	cout<<ans<<endl;
    	return 0;
    } 


  • 相关阅读:
    Vue Supermall蘑菇街API后端接口
    Vue UI库:ElementUI使用教程
    Python操作数据库,读取数据并按照json格式写入json文件
    css 轮播图
    ArcGIS Server密码重置
    JavaScript之箭头函数
    arcgis属性对比
    JavaScript之Promise
    很遥远
    请不要等到四十年后才明白
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11746061.html
Copyright © 2011-2022 走看看